计算字典中值的平均值 [英] working out an average of the values in a dictionary
本文介绍了计算字典中值的平均值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
到目前为止,我的字典是这样的:
My dictionary as of now is like this:
class_1 = {'Bob':[9,5,4,3,3,4], 'John':[5,5,7,3,6], 'Andy':[7,5,6,4,5], 'Harris':[3,4,2,3,2,3,2]}
我要使它看起来像这样:
What i am trying to make it look like is this:
class_1_average ={'Bob':[average of scores],'John':[average of scores].........
我该怎么做,以便即使分数更新,平均值也会随之更新.还有一种方法可以从分数中找到最高分数
How can i do this so that even when the scores updates the average also updates with it. And is there also a way to find the highest score out of the scores
推荐答案
尝试如下:
class_1_average = {key: sum(value)/len(value) for key, value in class_1.items()}
对于最大值:您可以对字典的值使用max内置函数
For max value: you can use max built-in function over the value of dictionary
max_each = {key: max(value) for key, value in class_1.items()}
这篇关于计算字典中值的平均值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文