Python列表追加行为 [英] Python list append behavior
本文介绍了Python列表追加行为的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
真的不知道如何提出问题...
Don't really know how to formulate the question...
假设我执行以下操作:
>>> l = [[]]*2
>>> l
[[], []]
>>> l[0].append(1)
>>> l
[[1], [1]]
为什么将1附加到两个列表中?
Why does 1 gets appended to both lists?
推荐答案
[[]]*2
是两个引用同一列表的列表.您要附加它,然后看到它两次.
[[]]*2
is a list of two references to the same list. You are appending to it and then seeing it twice.
这篇关于Python列表追加行为的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文