我对以下代码有一点问题。我首先创建一个二维数组,并用字典填充(实际上每个单元格的字典相同)。现在,当我更新字典中值的单元格更改时,整个数组中的所有字典都发生了变化,而不仅仅是array[0][0]
.请参阅下面的代码:
dict_cell = {'item1': 20, 'item2': 25}
width = 2
height = 2
array = []
for i in range(height):
row=[]
for j in range(width):
row.append(dict_cell)
array.append(row)
array[0][0]['item1'] =2
print array
我得到以下输出:
[[{'item2': 25, 'item1': 2},
{'item2': 25, 'item1': 2}],
[{'item2': 25, 'item1': 2},
{'item2': 25, 'item1': 2}]]
当我想拥有:
[[{'item2': 25, 'item1': 2},
{'item2': 25, 'item1': 20}],
[{'item2': 25, 'item1': 20},
{'item2': 25, 'item1': 20}]]
有什么建议吗? 注意:我不能使用numpy。
让我们通过一个小例子来探讨您的问题:
所以假设你有一个列表:
a=['something']
还有第二个列表,其中包含:
list_1=[a,a,a]
因此,当您这样做时:
a[0]="something_else"
您认为list_1
的输出是什么?
让我们检查一下:
a=['something']
list_1=[a,a,a]
a[0]="something_else"
print(list_1)
输出:
[['something_else'], ['something_else'], ['something_else']]
因为在python中变量不存储值,变量只是引用对象和对象存储值,所以在list_1所有变量都指向同一个对象:
检查:
for i in list_1:
print(id(i))
输出:
4329477768
4329477768
4329477768
在您的情况下:
dict_cell = {'item1': 20, 'item2': 25}
width = 2
height = 2
array = []
for i in range(height):
row=[]
for j in range(width):
row.append(dict_cell)
array.append(row)
array[0][0]['item1'] =2
for item in array:
if isinstance(item,list):
for sub_item in item:
print(id(sub_item))
输出:
4302653768
4302653768
4302653768
4302653768
因此,您可以看到列表中的所有变量都指向同一个字典,因此,如果您将任何内容更改为一个,它将影响主字典。
所以当你这样做时:
array[0][0]['item1'] =2
您不仅在修改数组的字典,实际上也在修改原始字典,让我们检查一下:
dict_cell = {'item1': 20, 'item2': 25}
width = 2
height = 2
array = []
print("before modification {}".format(dict_cell))
for i in range(height):
row=[]
for j in range(width):
row.append(dict_cell)
array.append(row)
array[0][0]['item1'] =2
print("after modification {}".format(dict_cell))
输出:
before modification {'item1': 20, 'item2': 25}
after modification {'item1': 2, 'item2': 25}
好的,我遇到了问题,但解决方案是什么?
尝试深拷贝:
from copy import deepcopy
dict_cell = {'item1': 20, 'item2': 25}
width = 2
height = 2
array = []
for i in range(height):
row=[]
for j in range(width):
row.append(deepcopy(dict_cell))
array.append(row)
array[0][0]['item1'] =2
print(array)
输出:
[[{'item1': 2, 'item2': 25}, {'item1': 20, 'item2': 25}], [{'item1': 20, 'item2': 25}, {'item1': 20, 'item2': 25}]]
为什么
deepcopy
为什么不copy
?
假设你有这样的字典:
dict_cell = {'item1': [20,34], 'item2': [25,9]}
使用此dict_cell运行代码,并得到输出:
[[{'item2': [25, 9], 'item1': 2}, {'item2': [25, 9], 'item1': [20, 34]}], [{'item2': [25, 9], 'item1': [20, 34]}, {'item2': [25, 9], 'item1': [20, 34]}]]
现在让我们尝试更改原始字典值:
dict_cell = {'item1': [20,34], 'item2': [25,9]}
width = 2
height = 2
array = []
for i in range(height):
row=[]
for j in range(width):
row.append(dict_cell.copy())
array.append(row)
array[0][0]['item1'] =2
for key,value in dict_cell.items():
value[0]='changed'
print(array,'n')
输出:
[[{'item1': 2, 'item2': ['changed', 9]}, {'item1': ['changed', 34], 'item2': ['changed', 9]}], [{'item1': ['changed', 34], 'item2': ['changed', 9]}, {'item1': ['changed', 34], 'item2': ['changed', 9]}]]
我们修改了 Orional Dict,但它更改了数组列表字典中的内容,因为这是字典的浅拷贝。 它复制了字典,但没有复制嵌套列表。
溶液:
使用深拷贝。
python的工作方式是,当你将dict_cell
附加到row
时,它会添加一个指向字典的指针。所以,实际上,你基本上是链接同一个词典很多次!因此,当您修改其中一个条目时,所有词典都会更改也就不足为奇了。
解决此问题的最简单更改是直接创建多个词典,使用dict_cell
作为模板。将 for 循环中的append
替换为以下内容:
row.append(dict_cell.copy())