Python 如何更改字典中值的顺序?
我试图改变字典中的值顺序。假设我有一本字典,里面有名字、班级和班级等级Python 如何更改字典中值的顺序?,python,dictionary,nested,Python,Dictionary,Nested,我试图改变字典中的值顺序。假设我有一本字典,里面有名字、班级和班级等级 classGrades={"Computer Science":{"Bob":[98,100,100,88],"Sue":[100,88,100,100],"Jill":[100,100,100,100]}, "English":{"Sue":[100,100,100,100,88],"Mary":[88,90,88,90,88],"John":[100,100,100,100,100],"Joe":[90,90,70,7
classGrades={"Computer Science":{"Bob":[98,100,100,88],"Sue":[100,88,100,100],"Jill":[100,100,100,100]},
"English":{"Sue":[100,100,100,100,88],"Mary":[88,90,88,90,88],"John":[100,100,100,100,100],"Joe":[90,90,70,70,80]},"
Chemistry":{"Bob":[98,100,100,88],"Sue":[88,88,88,88],"Jill":[100,100,100,100]}}
目标是更改表单,以便每个人的班级都有相应的等级。预期产出:
{"Bob":{"Computer Science":[98,100,100,88],"Chemistry":[98,100,100,88]},
"Sue":{"Computer Science":[100,88,100,100],"Chemistry":[88,88,88,88],"English":[100,100,100,100,88]},
"Jill":{"Computer Science":[100,100,100,100],"Chemistry":[100,100,100,100]},
"Mary":{"English":[88,90,88,90,88]},
"John":{"English":[100,100,100,100,100]},
"Joe":{"ENG110":[90,90,70,70,80]}}
它的格式不会完全如图所示,它只是一个大列表,但我制作了它,所以很明显它应该如何组织。我甚至不知道我是否知道从哪里开始。你可以这样做:
classGrades = {
"Computer Science": {"Bob": [98, 100, 100, 88], "Sue": [100, 88, 100, 100], "Jill": [100, 100, 100, 100]},
"English": {"Sue": [100, 100, 100, 100, 88], "Mary": [88, 90, 88, 90, 88], "John": [100, 100, 100, 100, 100],
"Joe": [90, 90, 70, 70, 80]},
"Chemistry": {"Bob": [98, 100, 100, 88], "Sue": [88, 88, 88, 88], "Jill": [100, 100, 100, 100]}}
result = {}
for _class, names in classGrades.items():
for name, grade in names.items():
result.setdefault(name, {})[_class] = grade
print(result)
输出
{'Mary': {'English': [88, 90, 88, 90, 88]}, 'Joe': {'English': [90, 90, 70, 70, 80]}, 'Sue': {'Computer Science': [100, 88, 100, 100], 'English': [100, 100, 100, 100, 88], 'Chemistry': [88, 88, 88, 88]}, 'Jill': {'Computer Science': [100, 100, 100, 100], 'Chemistry': [100, 100, 100, 100]}, 'Bob': {'Computer Science': [98, 100, 100, 88], 'Chemistry': [98, 100, 100, 88]}, 'John': {'English': [100, 100, 100, 100, 100]}}
你可以这样做:
classGrades = {
"Computer Science": {"Bob": [98, 100, 100, 88], "Sue": [100, 88, 100, 100], "Jill": [100, 100, 100, 100]},
"English": {"Sue": [100, 100, 100, 100, 88], "Mary": [88, 90, 88, 90, 88], "John": [100, 100, 100, 100, 100],
"Joe": [90, 90, 70, 70, 80]},
"Chemistry": {"Bob": [98, 100, 100, 88], "Sue": [88, 88, 88, 88], "Jill": [100, 100, 100, 100]}}
result = {}
for _class, names in classGrades.items():
for name, grade in names.items():
result.setdefault(name, {})[_class] = grade
print(result)
输出
{'Mary': {'English': [88, 90, 88, 90, 88]}, 'Joe': {'English': [90, 90, 70, 70, 80]}, 'Sue': {'Computer Science': [100, 88, 100, 100], 'English': [100, 100, 100, 100, 88], 'Chemistry': [88, 88, 88, 88]}, 'Jill': {'Computer Science': [100, 100, 100, 100], 'Chemistry': [100, 100, 100, 100]}, 'Bob': {'Computer Science': [98, 100, 100, 88], 'Chemistry': [98, 100, 100, 88]}, 'John': {'English': [100, 100, 100, 100, 100]}}
你可以用。由于defaultdict
是dict
的子类,因此通常不需要转换回常规dict
from collections import defaultdict
dd = defaultdict(lambda: defaultdict(list))
for subject, names in classGrades.items():
for name, grades in names.items():
dd[name][subject] = grades
结果:
print(dd)
defaultdict(<function __main__.<lambda>>,
{'Bob': defaultdict(list,
{'Chemistry': [98, 100, 100, 88],
'Computer Science': [98, 100, 100, 88]}),
'Jill': defaultdict(list,
{'Chemistry': [100, 100, 100, 100],
'Computer Science': [100, 100, 100, 100]}),
'Joe': defaultdict(list, {'English': [90, 90, 70, 70, 80]}),
'John': defaultdict(list, {'English': [100, 100, 100, 100, 100]}),
'Mary': defaultdict(list, {'English': [88, 90, 88, 90, 88]}),
'Sue': defaultdict(list,
{'Chemistry': [88, 88, 88, 88],
'Computer Science': [100, 88, 100, 100],
'English': [100, 100, 100, 100, 88]})})
打印(dd)
defaultdict(,
{'Bob':defaultdict(列表,
{'Chemistry':[9810010088],
"计算机科学":[9810010088]),,
“吉尔”:defaultdict(列表,
{'Chemistry':[100100100100],
"计算机科学":[100100100100]},,
'Joe':defaultdict(列表,{'English':[90,90,70,70,80]}),
'John':defaultdict(列表,{'English':[100100100100100]}),
'Mary':defaultdict(列表,{'English':[88,90,88,90,88]}),
“Sue”:defaultdict(列表,
{'Chemistry':[88,88,88,88],
"计算机科学":[10088100100],,
英语:[10010010088]})
您可以使用。由于defaultdict
是dict
的子类,因此通常不需要转换回常规dict
from collections import defaultdict
dd = defaultdict(lambda: defaultdict(list))
for subject, names in classGrades.items():
for name, grades in names.items():
dd[name][subject] = grades
结果:
print(dd)
defaultdict(<function __main__.<lambda>>,
{'Bob': defaultdict(list,
{'Chemistry': [98, 100, 100, 88],
'Computer Science': [98, 100, 100, 88]}),
'Jill': defaultdict(list,
{'Chemistry': [100, 100, 100, 100],
'Computer Science': [100, 100, 100, 100]}),
'Joe': defaultdict(list, {'English': [90, 90, 70, 70, 80]}),
'John': defaultdict(list, {'English': [100, 100, 100, 100, 100]}),
'Mary': defaultdict(list, {'English': [88, 90, 88, 90, 88]}),
'Sue': defaultdict(list,
{'Chemistry': [88, 88, 88, 88],
'Computer Science': [100, 88, 100, 100],
'English': [100, 100, 100, 100, 88]})})
打印(dd)
defaultdict(,
{'Bob':defaultdict(列表,
{'Chemistry':[9810010088],
"计算机科学":[9810010088]),,
“吉尔”:defaultdict(列表,
{'Chemistry':[100100100100],
"计算机科学":[100100100100]},,
'Joe':defaultdict(列表,{'English':[90,90,70,70,80]}),
'John':defaultdict(列表,{'English':[100100100100100]}),
'Mary':defaultdict(列表,{'English':[88,90,88,90,88]}),
“Sue”:defaultdict(列表,
{'Chemistry':[88,88,88,88],
"计算机科学":[10088100100],,
英语:[10010010088]})
如果只需要使用for循环,而不需要使用任何方法,则可以执行以下操作:
new_dict = {}
for subject,students in classGrades.items():
for names, marks in students.items():
if names in new_dict:
new_dict[names].update({subject:marks})
else:
new_dict[names] = {subject:marks}
print(new_dict)
输出:
{'Bob': {'Computer Science': [98, 100, 100, 88], 'Chemistry': [98, 100, 100, 88]}, 'Sue': {'Computer Science': [100, 88, 100, 100], 'English': [100, 100, 100, 100, 88], 'Chemistry': [88, 88, 88, 88]}, 'Jill': {'Computer Science': [100, 100, 100, 100], 'Chemistry': [100, 100, 100, 100]}, 'Mary': {'English': [88, 90, 88, 90, 88]}, 'John': {'English': [100, 100, 100, 100, 100]}, 'Joe': {'English': [90, 90, 70, 70, 80]}}
如果只需要使用for循环,而不需要使用任何方法,则可以执行以下操作:
new_dict = {}
for subject,students in classGrades.items():
for names, marks in students.items():
if names in new_dict:
new_dict[names].update({subject:marks})
else:
new_dict[names] = {subject:marks}
print(new_dict)
输出:
{'Bob': {'Computer Science': [98, 100, 100, 88], 'Chemistry': [98, 100, 100, 88]}, 'Sue': {'Computer Science': [100, 88, 100, 100], 'English': [100, 100, 100, 100, 88], 'Chemistry': [88, 88, 88, 88]}, 'Jill': {'Computer Science': [100, 100, 100, 100], 'Chemistry': [100, 100, 100, 100]}, 'Mary': {'English': [88, 90, 88, 90, 88]}, 'John': {'English': [100, 100, 100, 100, 100]}, 'Joe': {'English': [90, 90, 70, 70, 80]}}