我在嵌套词典中分配时遇到了困难。我有一个嵌套字典,有两所学校,三位老师和四位学生。这只是一个玩具的例子,所以我并不关心每所学校的学生和老师是一样的。但是,我确实希望每个等级都不同。但是,使用下面的代码,所有班级和所有学校的所有学生的成绩与最后输入的学校/班级相同。
/**
* @ORM\Column(type="integer", nullable=true)
*/
private $projectDetailsId;
这就是我想要的数据:
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]
在stackoverflow上似乎有类似的问题,但它们无法帮助我解决这里发生的事情。谢谢。
答案 0 :(得分:1)
您(重新)在整个字典中使用相同的grade_dict
对象。
在每次迭代时创建单独的对象。此外,您不需要在每个键上初始化值:
for i in students:
for j in teachers:
teachers_dict[j] = {}
for k in schools:
school_dict[k] = teachers_dict
OTOH,您应该通过使用嵌套school_dict
声明collections.defaultdict
来避免使用 for 预先分配值/ 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)]
答案 1 :(得分:1)
您可以使用词典理解:
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]}}}