嵌套字典分配错误 - 对可变字典的混淆

nested dictionary assignment error - confusion on mutable dictionaries

我在嵌套字典中遇到困难。我有一个嵌套字典,里面有两个学校、三个老师和四个学生。这只是一个玩具示例,所以我不关心每所学校的学生和老师是否相同。但是,我确实希望每个年级都不同。然而,使用下面的代码,所有 classes 和所有学校的所有学生的成绩都与最后一所学校/class 输入的成绩相同。

schools = ['School A', 'School B']
teachers = ['mr. smith', 'ms. jones', 'mr. kronk']
students = ['Adam', 'Nick', 'Jeff', 'Dave']
grade_dict ={}
for i in students:
    grade_dict[i] = ''
    for j in teachers:
        teachers_dict[j] = grade_dict
        for k in schools:
            school_dict[k] = teachers_dict
for k in schools:
    for j in teachers:
        for i in students:
            a = [randint(70, 100), randint(70, 100), randint(70, 100)]
            school_dict[k][j][i] = a

这就是我想要的数据:

School A mr. smith Adam [71, 72, 82]
School A mr. smith Nick [86, 80, 96]
School A mr. smith Jeff [77, 70, 83]
School A mr. smith Dave [79, 83, 98]
School A ms. jones Adam [70, 98, 87]
School A ms. jones Nick [80, 94, 76]
School A ms. jones Jeff [79, 82, 93]
School A ms. jones Dave [90, 97, 85]
School A mr. kronk Adam [93, 75, 95]
School A mr. kronk Nick [80, 82, 72]
School A mr. kronk Jeff [75, 72, 89]
School A mr. kronk Dave [86, 92, 98]
School B mr. smith Adam [89, 77, 84]
School B mr. smith Nick [93, 71, 74]
School B mr. smith Jeff [78, 83, 83]
School B mr. smith Dave [72, 83, 70]
School B ms. jones Adam [82, 100, 78]
School B ms. jones Nick [80, 89, 100]
School B ms. jones Jeff [91, 81, 77]
School B ms. jones Dave [86, 86, 74]
School B mr. kronk Adam [82, 73, 100]
School B mr. kronk Nick [81, 71, 74]
School B mr. kronk Jeff [92, 100, 90]
School B mr. kronk Dave [86, 97, 85]

不幸的是,这就是相应字典的样子:

{'School A': {'mr. kronk': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'mr. smith': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'ms. jones': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]}},
 'School B': {'mr. kronk': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'mr. smith': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]},
  'ms. jones': {'Adam': [86, 89, 94],
   'Dave': [74, 85, 86],
   'Jeff': [79, 94, 70],
   'Nick': [90, 80, 97]}}}

似乎在 Whosebug 上有类似的问题,但他们不能完全帮助我解决这里发生的问题。谢谢。

您在整个字典中(重新)使用相同的 grade_dict 对象。 在每次迭代中创建单独的对象。此外,您不需要在每个键处初始化值:

for i in students:
    for j in teachers:
        teachers_dict[j] = {}
        for k in schools:
            school_dict[k] = teachers_dict

OTOH,您应该通过使用嵌套 collections.defaultdict.

声明 school_dict 来避免使用 for 预分配 values/dicts
from collections import defaultdict

school_dict = defaultdict(lambda: school_dict) # dict nesting is recursive

for k in schools:
    for j in teachers:
        for i in students:
            school_dict[k][j][i] = [randint(70, 100), randint(70, 100), randint(70, 100)]

您可以使用字典理解:

from random import randint
schools = ['School A', 'School B']
teachers = ['mr. smith', 'ms. jones', 'mr. kronk']
students = ['Adam', 'Nick', 'Jeff', 'Dave']

final_dict = {school:{teacher:{student:[randint(70, 100), randint(70, 100), randint(70, 100)] for student in students} for teacher in teachers} for school in schools}

输出:

{'School A': {'mr. smith': {'Nick': [75, 81, 86], 'Dave': [85, 88, 84], 'Adam': [78, 95, 99], 'Jeff': [74, 95, 81]}, 'ms. jones': {'Nick': [76, 86, 92], 'Dave': [92, 100, 95], 'Adam': [98, 99, 90], 'Jeff': [74, 100, 95]}, 'mr. kronk': {'Nick': [84, 97, 79], 'Dave': [93, 91, 89], 'Adam': [83, 98, 79], 'Jeff': [89, 83, 99]}}, 'School B': {'mr. smith': {'Nick': [70, 78, 89], 'Dave': [81, 95, 92], 'Adam': [95, 100, 91], 'Jeff': [91, 83, 82]}, 'ms. jones': {'Nick': [94, 85, 75], 'Dave': [99, 77, 94], 'Adam': [79, 97, 92], 'Jeff': [91, 84, 79]}, 'mr. kronk': {'Nick': [81, 90, 86], 'Dave': [72, 95, 82], 'Adam': [80, 73, 77], 'Jeff': [88, 88, 95]}}}