0
考虑这个例子:追加到嵌套列表列出的清单内
>>> result = [[]] * 8
>>> result
[[], [], [], [], [], [], [], []]
>>> result[0]
[]
>>> result[0].append("foo")
>>> result # wtf? expected result: [['foo'], [], [], [], [], [], [], []]
[['foo'], ['foo'], ['foo'], ['foo'], ['foo'], ['foo'], ['foo'], ['foo']]
我非常这一困惑。也许我不明白如何使用append
。我将如何附加到列表中的i
嵌套列表中?
请参阅[本问题](http://stackoverflow.com/questions/19427735/where-goes-wrong-in-this-list-manipulation/19427770#19427770)。 – mdml
您正在复制对同一列表的引用。 '结果[0]'与'result [1]','result [2]'等是相同的对象。 –
啊......我明白了。非常微妙。谢谢,投票结束我的问题重复。 –