Python 3.4 - 如何获得字典值的平均值?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/30687244/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Python 3.4 - How to get the average of dictionary values?
提问by Vladimir Dimov
I have the following dictionary:
我有以下字典:
StudentGrades = {
'Ivan': [4.32, 3, 2],
'Martin': [3.45, 5, 6],
'Stoyan': [2, 5.67, 4],
'Vladimir': [5.63, 4.67, 6]
}
I want to make a function that prints the average of the grades of the students, i.e. the average of the values, but I have no idea how. Can you help me please?
我想制作一个打印学生成绩平均值的函数,即值的平均值,但我不知道如何。你能帮我吗?
采纳答案by Jiby
This answer was intended for Python2, which is now dead
这个答案是为 Python2 准备的,现在已经死了
Okay, so let's iterate over all dictionary keys and average the items:
好的,让我们遍历所有字典键并平均项目:
avgDict = {}
for k,v in StudentGrades.iteritems():
# v is the list of grades for student k
avgDict[k] = sum(v)/ float(len(v))
In Python3, the
iteritems()
method is no longer necessary, can useitems()
directly.
在Python3中,该
iteritems()
方法不再需要,items()
直接使用即可。
now you can just see :
现在你可以看到:
avgDict
Out[5]:
{'Ivan': 3.106666666666667,
'Martin': 4.816666666666666,
'Stoyan': 3.89,
'Vladimir': 5.433333333333334}
From your question I think you're queasy about iteration over dicts, so here is the same with output as a list :
从你的问题来看,我认为你对 dicts 的迭代感到不安,所以这里的输出与列表相同:
avgList = []
for k,v in StudentGrades.iteritems():
# v is the list of grades for student k
avgDict.append(sum(v)/ float(len(v)))
Be careful though : the order of items in a dictionary is NOT guaranteed; this is, the order of key/values when printing or iterating on the dictionary is not guaranteed (as dicts are "unsorted"). Looping over the same identical dictionary object(with no additions/removals) twice is guaranteed to behave identically though.
不过要小心:不能保证字典中项目的顺序;也就是说,不能保证在字典上打印或迭代时键/值的顺序(因为字典是“未排序的”)。两次循环相同的字典对象(没有添加/删除)保证行为相同。
回答by daniel451
from scipy import mean
map(lambda x: mean(StudentGrades[x]), StudentGrades)
Generates this output:
生成此输出:
[3.1066666666666669,
3.8900000000000001,
5.4333333333333336,
4.8166666666666664]
If you prefer a non-scipy solution one could use sum
and len
like supposed by Jiby:
如果您更喜欢非 scipy 解决方案,可以使用sum
并len
喜欢 Jiby 的假设:
map(lambda x: sum(StudentGrades[x])/len(StudentGrades[x]), StudentGrades)
EDIT:I am terribly sorry, I forgot you want a Python 3.4 solution, therefore (because you would get a map object returned) you need, for example, an additional list command:
编辑:非常抱歉,我忘了你想要一个 Python 3.4 解决方案,因此(因为你会得到一个地图对象返回)你需要,例如,一个额外的列表命令:
from scipy import mean
list(map(lambda x: mean(StudentGrades[x]), StudentGrades))
This will return the desired output.
这将返回所需的输出。
回答by Padraic Cunningham
If you don't want to do the simple calculation use statistics.mean:
如果您不想进行简单的计算,请使用 statistics.mean:
from statistics import mean
StudentGrades = {
'Ivan': [4.32, 3, 2],
'Martin': [3.45, 5, 6],
'Stoyan': [2, 5.67, 4],
'Vladimir': [5.63, 4.67, 6]
}
for st,vals in StudentGrades.items():
print("Average for {} is {}".format(st,mean(vals)))