[英]Python: “int” object is not iteratable in nested enumerate(list)
I am trying to iterate over every element of list b :我正在尝试遍历列表 b 的每个元素:
a = [1, 2, 3, 4]
b = [1, 2, 3, 4]
for cnt1, a in enumerate(a):
print ("a:",cnt1, a)
for cnt2, b in enumerate(b):
print ("b:", cnt2, b)
However, I always get a " TypeError: 'int' object is not iterable" at the inner loop for the second iteration of a.但是,对于 a 的第二次迭代,我总是在内部循环中收到“ TypeError: 'int' object is not iterable”。
Expected :预期:
a: 0 1一:0 1
b: 0 1乙:0 1
b: 1 2乙:1 2
b: 2 3乙:2 3
b: 3 4乙:3 4
a: 1 2一:1 2
b: 0 1乙:0 1
... ...
b: 3 4乙:3 4
a: 2 3一:2 3
... ...
Actual :实际:
a: 0 1一:0 1
b: 0 1乙:0 1
b: 1 2乙:1 2
b: 2 3乙:2 3
b: 3 4乙:3 4
a: 1 2一:1 2
TypeError: 'int' object is not iterable at: for cnt2, b in enumerate(b):类型错误:'int' 对象不可迭代于:对于 cnt2,enumerate(b) 中的 b:
As Iain pointed out in the comments, You're redefining a
and b
in the loop, this will fix the issue.正如 Iain 在评论中指出的,您正在循环中重新定义
a
和b
,这将解决问题。
a = [1, 2, 3, 4]
b = [1, 2, 3, 4]
for cnt1, ele1 in enumerate(a):
print ("a:",cnt1, ele1)
for cnt2, ele2 in enumerate(b):
print ("b:", cnt2, ele2)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.