我创建了以下包含False的列表列表:
array=[[False] * 3] * 5
如何只更改值?
array[1][0]='change only this'
目前结果是
[['change only this', False, False],
['change only this', False, False],
['change only this', False, False],
['change only this', False, False],
['change only this', False, False]]
这是不希望的。我理解每列是一个单独的对象[False] * 3]。我怎样才能将它们彼此分离。我是否需要以某种方式使它们变得不可变?
答案 0 :(得分:2)
你可以写
array = [[False] * 3 for _ in range(5)]
这将创建五个单独的三元素列表,并将引用存储在array
。