我无法理解为什么循环变量不会改变,但我明确地尝试了。所以这是我的代码:
a=[1,2,3]
b=["a","b","c"]
d=[a,b]
for i in d:
for a in i:
a*2
print(a)
当我跑步时,我看到:
1
2
3
a
b
c
取而代之的是:
2
4
6
aa
bb
cc
答案 0 :(得分:0)
因为您没有为变量赋值。如果你做a = a * 2而不只是a * 2,它会改变。在python shell中试试这个:
>>> a=[1,2,3]
>>> b=["a","b","c"]
>>> d=[a,b]
>>> for i in d:
... for a in i:
... a=a*2
... print(a)
...
2
4
6
aa
bb
cc
>>>
答案 1 :(得分:0)
a=[1,2,3]
b=["a","b","c"]
d=[a,b]
for i in d:
for a in i:
a=a*2 # change this line like that
print(a)
答案 2 :(得分:0)
要在迭代a
时更改i
,您必须将值分配给变量。
所以代替
for a in i:
a*2
print(a)
试
for a in i:
a = a*2
print(a)