迭代字典中的列表

问题描述 投票:0回答:1

我是Python的新手,我想遍历字典内列表中的字典(令人困惑,我知道)。

my_dict = {"John": [{"class": "math", "score": 100, "year": 2014}, {"class": "english", "score": 85, "year": 2015}], 
"Timmy": [{"class": "math", "score": 87, "year": 2014}, {"class": "english", "score": 91, "year": 2015}], 
"Sally":[{"class": "math", "score": 95, "year": 2014}]}

我需要创建一个包含学生姓名及其综合分数的新词典(Sally只有一个分数)。>>

输出看起来像:

new_dict = {"John": 185, "Timmy": 178, "Sally": 95}

任何帮助或指导将不胜感激!

我是Python的新手,我想遍历字典列表内的字典(我知道这很混乱)。 my_dict = {“ John”:[{“ class”:“ math”,“ score”:100,“ year”:2014},{“ class”:“ ...

python list dictionary list-comprehension dictionary-comprehension
1个回答
0
投票

使用字典理解:

© www.soinside.com 2019 - 2024. All rights reserved.