我想在下面的列表中打印每个值。我还需要知道索引。所以我想使用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)
但是不幸的是,我遇到了这个错误:'int'对象是不可迭代的。
答案 0 :(得分:4)
不需要嵌套循环,您可以直接解压缩:
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)
输出
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