我正在尝试更新内部dictonary中的值,但它正在更新两者,我尝试了很多,但仍然面临同样的问题。提前致谢。
arr = [1,2]
d_int = {'a':False, 'e':False, 'i':False, 'o':False, 'u':False}
d_main ={i : d_int for i in range(len(arr))}
print(d_main)
d_main[0]['a'] = True
d_main[1]['i'] = True
print(d_main)
结果:
{0: {'a': True, 'e': False, 'i': True,
'o': False, 'u': False},
1: {'a': True, 'e': False, 'i': True,
'o': False, 'u': False}
}
需要这样的东西:
{0: {'a': True, 'e': False, 'i': False,
'o': False, 'u': False},
1: {'a': False, 'e': False, 'i': True,
'o': False, 'u': False}
}
这是因为d_main
的所有值都指向相同的dict对象d_int
。对其中任何一个的更改都将反映在所有参考文献中。 Further read
要避免此问题,请确保使用.copy
等复制方法在列表解析期间创建dict对象的新副本。
arr = [1,2]
d_int = {'a':False, 'e':False, 'i':False, 'o':False, 'u':False}
d_main ={i : d_int.copy() for i in range(len(arr))} #modified.
print(d_main)
d_main[0]['a'] = True
d_main[1]['i'] = True
print(d_main)
#Output:
{0: {'a': True, 'e': False, 'i': False, 'o': False, 'u': False}, 1: {'a': False, 'e': False, 'i': True, 'o': False, 'u': False}}
简短的解决方法:将d_int更改为d_int.copy(),您将获得输出。您正在引用同一个对象。看看这两种情况下id的差异:
arr = [1,2]
d_int = {'a':False, 'e':False, 'i':False, 'o':False, 'u':False}
d_main ={i : d_int for i in range(len(arr))}
print(list(map(id,d_main.values())))
返回:
[140545244867248, 140545244867248] # <-- they are the same
arr = [1,2]
d_int = {'a':False, 'e':False, 'i':False, 'o':False, 'u':False}
d_main ={i : d_int.copy() for i in range(len(arr))} # <-- .copy() added
print(list(map(id,d_main.values())))
返回:
[140545244796464, 140545244798480]
如果你是新手,没有任何编程语言的编程经验,为了理解问题发生的原因,你最好先了解C编程语言中的指针是什么。
copy.deepcopy
是最好的。 dict.copy
在深层嵌套词典中遇到同样的麻烦。如下。
a = {"1": {"2": 2, "3": 3}, "4":4}
c = {1:a.copy()}
b = {1:a.copy()}
c[1]['1']['2'] = 5
c[1]['4'] = 40
print(c)
# {1: {'1': {'3': 3, '2': 5}, '4': 40}}
print(b)
# {1: {'1': {'3': 3, '2': 5}, '4': 4}}
但copy.deepcopy
适用于深嵌套词典。
from copy import deepcopy
a = {"1": {"2": 2, "3": 3}, "4":4}
c = {1: deepcopy(a)}
b = {1: deepcopy(a)}
c[1]['1']['2'] = 5
c[1]['4'] = 40
print(c)
# {1: {'1': {'2': 5, '3': 3}, '4': 40}}
print(b)
# {1: {'1': {'2': 2, '3': 3}, '4': 4}}
实际上,这个问题不仅发生在嵌套的dict,其他类型也是如此,喜欢嵌套列表,带有dict属性的类等等,而且copy.deepcopy
运行良好。