【发布时间】:2020-07-18 00:06:31
【问题描述】:
我是 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 list dictionary list-comprehension dictionary-comprehension