python how to change element in nested list [duplicate]

霸气de小男生 提交于 2019-12-01 10:49:42

You have a list of the same [0, 0] element 10 times here:

l=[[0,0]]*10

Any time you modify one, it modifies them all, because they're the same list.

One safe way to make them unique would be:

l = [[0, 0] for _ in range(10)]

One easy way to check would be to print the id of each, which is the memory address where it's stored:

>>> for element in l:
...     print id(element)
...
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
34669128
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!