简体   繁体   中英

Looping over multiple lists with enumerate

It looks like enumerate and zip don't work together in Python 3?

alist = ['a1', 'a2', 'a3']
blist = ['b1', 'b2', 'b3']

for i, a, b in enumerate(zip(alist, blist)):
    print(i, a, b)

Returns 'int' object is not callable

Add () around a,b . The unpacking of the values is for the enumerate function which returns tuples of size two: index and value. If you further want to unpack the value item then as below:

for i, (a, b) in enumerate(zip(alist, blist)):
    print(i, a, b)

Since zip returns tuples, you can also do:

for i, t in enumerate(zip(alist, blist)):
    print(i, t[0], t[1])

Or:

for i, t in enumerate(zip(alist, blist)):
    print(i, *t)

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