[英]Can't iterate to multiple list while using enumerate in python
I wanted to print each value in the list below. 我想在下面的列表中打印每个值。 I also need to know the index.
我还需要知道索引。 so I wanted to use enumarate:
所以我想使用enumarate:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3),
(10, 5, 6), (10, 0, 3), (10, 10, 3), (10, 40, 6), (10, 45, 5), (10, 50, 6)]
for i, measurment in enumerate(a):
for a,b,c in measurment:
print( a,b,c)
But unfortunately i am having this error: 'int' object is not iterable. 但是不幸的是我遇到了这个错误:'int'对象不是可迭代的。
No need for the nested loop, you can unpack directly: 无需嵌套循环,可以直接解压缩:
a = [(10, 0, 3), (10, 10, 6), (10, 15, 4), (10, 20, 5), (10, 3, 3),
(10, 5, 6), (10, 0, 3), (10, 10, 3), (10, 40, 6), (10, 45, 5), (10, 50, 6)]
for i, (a, b, c) in enumerate(a):
print(i, a, b, c)
Output 产量
0 10 0 3
1 10 10 6
2 10 15 4
3 10 20 5
4 10 3 3
5 10 5 6
6 10 0 3
7 10 10 3
8 10 40 6
9 10 45 5
10 10 50 6
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.