猿问

如何更改字典字典中的值顺序?

我正在尝试更改字典中值的顺序。假设我有一本字典,与名称、班级和班级等级相对应


 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]}}

它的格式不会完全如图所示,它只是一个大列表,但我这样做了,所以很明显它应该如何组织。我什至不确定我是否知道从哪里开始。


浮云间
浏览 313回答 3
3回答

翻翻过去那场雪

如果您只需要使用 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]}}

汪汪一只猫

你可以这样做: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] = gradeprint(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]}}

缥缈止盈

您可以使用collections.defaultdict. 由于defaultdict是 的子类,dict因此通常无需转换回常规dict.from collections import defaultdictdd = defaultdict(lambda: defaultdict(list))for subject, names in classGrades.items():&nbsp; &nbsp; for name, grades in names.items():&nbsp; &nbsp; &nbsp; &nbsp; dd[name][subject] = grades结果:print(dd)defaultdict(<function __main__.<lambda>>,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; {'Bob': defaultdict(list,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'Chemistry': [98, 100, 100, 88],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Computer Science': [98, 100, 100, 88]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Jill': defaultdict(list,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'Chemistry': [100, 100, 100, 100],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Computer Science': [100, 100, 100, 100]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Joe': defaultdict(list, {'English': [90, 90, 70, 70, 80]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'John': defaultdict(list, {'English': [100, 100, 100, 100, 100]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Mary': defaultdict(list, {'English': [88, 90, 88, 90, 88]}),&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;'Sue': defaultdict(list,&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp;{'Chemistry': [88, 88, 88, 88],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'Computer Science': [100, 88, 100, 100],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'English': [100, 100, 100, 100, 88]})})
随时随地看视频慕课网APP

相关分类

Python
我要回答