简体   繁体   中英

loop for tuple of list on python

I have tuple of lists. The lengths of the lists are same each other. For example:

tol = ([1,2,3], [4,5,6])

I'd like to loop all pair of two lists like:

for v1, v2 in some_operation(tol):
    print "(%f, %f)" % (v1, v2)

The above code should print (1,4)\\n(2,5)\\n(3,6)\\n .

One (little dirty) way is use zip

for v1, v2 in zip(tol[0], tol[1]):
    print...

Could you show me more simple way?

Using Zip would be simple and clean

tol = ([1,2,3], [4,5,6])

for v1, v2 in zip(*tol):
    print "(%d, %d)" % (v1, v2)

As you expect the output would be (1,4)\\n(2,5)\\n(3,6)\\n .

for v in zip(*tol):
    print "(%f, %f)" % (v[0], v[1])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM