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

时间:2018-11-02 16:51:27

标签: 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,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]}}

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

3 个答案:

答案 0 :(得分:2)

您可以执行以下操作:

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

答案 1 :(得分:1)

如果只需要用于循环而无需使用方法,则可以执行以下操作:

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

答案 2 :(得分:0)

您可以使用collections.defaultdict。由于defaultdictdict的子类,因此通常无需转换回常规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]})})