Python 为什么嵌套字典填写错误?

Python 为什么嵌套字典填写错误?,python,dictionary,nested,Python,Dictionary,Nested,我有一个这样设计的空嵌套字典: {'PEAR': {'GREEN': [], 'YELLOW': [], 'RED': []}, 'APPLE': {'GREEN': [], 'YELLOW': [], 'RED': []}, 'COURGETTE': {'GREEN': [], 'YELLOW': [], 'RED': []}} 这是我的代码: dictSub = {'RED' : [], 'GREEN' : [], 'YELLOW' : []} arrMainCodes = ['APPLE

我有一个这样设计的空嵌套字典:

{'PEAR': {'GREEN': [], 'YELLOW': [], 'RED': []}, 'APPLE': {'GREEN': [], 'YELLOW': [], 'RED': []}, 'COURGETTE': {'GREEN': [], 'YELLOW': [], 'RED': []}}
这是我的代码:

dictSub = {'RED' : [], 'GREEN' : [], 'YELLOW' : []}
arrMainCodes = ['APPLE', 'PEAR', 'COURGETTE']
dictAll = {}
numbers = {1 : [1, 2, 3], 2 : [4, 56, 7], 3 : [8, 2, 10]}

for item in arrMainCodes:
    dictAll[item] = dictSub
print(dictAll)
dictAll['PEAR']['RED'].append(478)
dictAll['PEAR']['RED'].append(47)
dictAll['PEAR']['RED'].append(8)
print(dictAll)
ITEM = [478, 7, 56]
for i in ITEM:
    if i not in dictAll['PEAR']['RED']:
        dictAll['PEAR']['RED'].append(i)
print(dictAll)
我试图以这样的方式填充它,即只填充键
'PEAR'
的子列表
'RED'
。但是,我的结果如下所示:

{'PEAR': {'GREEN': [], 'YELLOW': [], 'RED': [478, 47, 8, 7, 56]}, 'APPLE': {'GREEN': [], 'YELLOW': [], 'RED': [478, 47, 8, 7, 56]}, 'COURGETTE': {'GREEN': [], 'YELLOW': [], 'RED': [478, 47, 8, 7, 56]}}
正如您所看到的,所有的
'RED'
都已填充,而我只希望填充红梨


我做错了什么?这怎么解决呢?

它们都是对一本词典的引用。您的前四条语句是唯一真正创建新字典对象的语句;您的
for
循环只会创建其他名称,这些名称都引用同一个名称

您可以通过替换此分配来解决此问题:

dictAll[item] = dictSub
为此:

dictAll[item] = dictSub.copy()
这将为您提供单独的词典,但每个词典仍将引用相同的列表。要确保所有内容都是新副本,请改用
deepcopy()

dictAll[item] = dictSub.deepcopy()

它们都是对一本词典的引用。您的前四条语句是唯一真正创建新字典对象的语句;您的
for
循环只会创建其他名称,这些名称都引用同一个名称

您可以通过替换此分配来解决此问题:

dictAll[item] = dictSub
为此:

dictAll[item] = dictSub.copy()
这将为您提供单独的词典,但每个词典仍将引用相同的列表。要确保所有内容都是新副本,请改用
deepcopy()

dictAll[item] = dictSub.deepcopy()

问题是python。Python从不复制任何对象。因此,无论何时分配dict或数组,都会保留引用,无论何时更改引用,更改都会反映在所有引用中。 你可以这样做

arrMainCodes = ['APPLE', 'PEAR', 'COURGETTE']
dictAll = {}
numbers = {1 : [1, 2, 3], 2 : [4, 56, 7], 3 : [8, 2, 10]}

for item in arrMainCodes:
    dictAll[item]={'RED' : [], 'GREEN' : [], 'YELLOW' : []}
print(dictAll)
dictAll['PEAR']['RED'].append(478)
dictAll['PEAR']['RED'].append(47)
dictAll['PEAR']['RED'].append(8)
print(dictAll)
ITEM = [478, 7, 56]
for i in ITEM:
    if i not in dictAll['PEAR']['RED']:
        dictAll['PEAR']['RED'].append(i)
print(dictAll)

这样每次都会用新列表创建单独的dict。

问题在于python。Python从不复制任何对象。因此,无论何时分配dict或数组,都会保留引用,无论何时更改引用,更改都会反映在所有引用中。 你可以这样做

arrMainCodes = ['APPLE', 'PEAR', 'COURGETTE']
dictAll = {}
numbers = {1 : [1, 2, 3], 2 : [4, 56, 7], 3 : [8, 2, 10]}

for item in arrMainCodes:
    dictAll[item]={'RED' : [], 'GREEN' : [], 'YELLOW' : []}
print(dictAll)
dictAll['PEAR']['RED'].append(478)
dictAll['PEAR']['RED'].append(47)
dictAll['PEAR']['RED'].append(8)
print(dictAll)
ITEM = [478, 7, 56]
for i in ITEM:
    if i not in dictAll['PEAR']['RED']:
        dictAll['PEAR']['RED'].append(i)
print(dictAll)

这将每次使用新列表创建单独的dict。

我使用了dictSub.copy(),但它仍然给了我相同的结果。因为每次我猜测时,里面的列表都引用相同的内容。在这种情况下,使用
deepcopy
而不仅仅是
copy
。我使用了dictSub.copy(),但它仍然给了我同样的结果。因为每次我猜的时候里面的列表都引用了相同的东西。在这种情况下,使用
deepcopy
,而不仅仅是
copy