for i in range(10):
d={}
if i < 5:
x="test"
d[x]=[1,2]
print(d)
else:
print(x)
print(d)
为什么结果是:
{'test': [1, 2]}
{'test': [1, 2]}
{'test': [1, 2]}
{'test': [1, 2]}
{'test': [1, 2]}
test
{}
test
{}
test
{}
test
{}
test
{}
而不是后面5个也是{'test': [1, 2]}
...全文
2482打赏收藏
python字典问题
for i in range(10): d={} if i < 5: x="test" d[x]=[1,2] print(d) else: print(x) print(d) 为什么结果是: {'test': [1, 2]} {'test': [1, 2]} {'test': [1, 2]} {'test': [1, 2]} {'test': [1, 2]} test {} test {} test {} test {} test {} 而不是后面5个也是{'test': [1, 2]}