如何使用python从数据结构中提取字典?

Jin*_*ong 0 python python-3.x

因此,这是一个名为grade_lists的表,该表将学生的姓名映射到他们的考试成绩列表。等级应从字符串转换为整数。例如,grade_lists ['Thorny'] == [100,90,80]。有人能帮我吗?

 grades = [ #here is the table
    ['Student', 'Exam 1', 'Exam 2', 'Exam 3'],
    ['Thorny', '100', '90', '80'],
    ['Mac', '88', '99', '111'],
    ['Farva', '45', '56', '67'],
    ['Rabbit', '59', '61', '67'],
    ['Ursula', '73', '79', '83'],
    ['Foster', '89', '97', '101']
]
Run Code Online (Sandbox Code Playgroud)

Chr*_*ris 5

只需使用以下命令遍历列表dict

grades_dict = dict((x[0],x[1:]) for x in grades[1:])
grades_dict
Run Code Online (Sandbox Code Playgroud)

输出:

{'Farva': ['45', '56', '67'],
 'Foster': ['89', '97', '101'],
 'Mac': ['88', '99', '111'],
 'Rabbit': ['59', '61', '67'],
 'Thorny': ['100', '90', '80'],
 'Ursula': ['73', '79', '83']}
Run Code Online (Sandbox Code Playgroud)

grades_dict['Thorny']

['100', '90', '80']
Run Code Online (Sandbox Code Playgroud)