简体   繁体   English

使用枚举遍历多个列表

[英]Looping over multiple lists with enumerate

It looks like enumerate and zip don't work together in Python 3?看起来枚举和 zip 在 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返回 'int' object 不可调用

Add () around a,b .a,b周围添加() The unpacking of the values is for the enumerate function which returns tuples of size two: index and value.值的解包是针对enumerate function 的,它返回大小为 2 的元组:索引和值。 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:由于zip返回元组,您还可以执行以下操作:

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)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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