简体   繁体   English

如果某个键相同,则获取所有值的平均值

[英]Get the average of all values if a certain key is the same

So I have this list of dictionaries: 所以我有这个字典列表:

l = [{'COUNTRY': 'UK', 'CREDITS': '54'}, {'COUNTRY': 'PT', 'CREDITS': '100'}, {'COUNTRY': 'FR', 'CREDITS': '20'}, {'COUNTRY': 'UK', 'CREDITS': '30'}, {'COUNTRY': 'UK', 'CREDITS': '15'}, {'COUNTRY': 'PT', 'CREDITS': '35'}, {'COUNTRY': 'FR', 'CREDITS': '30'}]

I need to get the average of the credits of each country. 我需要获得每个国家的平均学分。 The output should be something like this: 输出应该是这样的:

l2 = {'UK': '102', 'PT': '67.5', 'FR': '25'}

Is there any simple and easy way to implement this? 是否有任何简单的方法来实现这一目标?

I would create a defaultdict first to gather the values in a list of integers under the "COUNTRY" key. 我将首先创建一个defaultdict ,以在“ COUNTRY”键下的整数列表中收集值。

Then I'll create a dict comprehension, performing the mean: 然后,我将创建一个dict理解,执行均值:

l = [{'COUNTRY': 'UK', 'CREDITS': '54'}, {'COUNTRY': 'PT', 'CREDITS': '100'}, {'COUNTRY': 'FR', 'CREDITS': '20'}, {'COUNTRY': 'UK', 'CREDITS': '30'}, {'COUNTRY': 'UK', 'CREDITS': '15'},
 {'COUNTRY': 'PT', 'CREDITS': '35'}, {'COUNTRY': 'FR', 'CREDITS': '30'}]

import collections

d = collections.defaultdict(list)
for s in l:
    d[s["COUNTRY"]].append(int(s["CREDITS"]))

result = {k:sum(v)/len(v) for k,v in d.items()}

print(result)

result: 结果:

{'UK': 33.0, 'PT': 67.5, 'FR': 25.0}

note that 1) your expected result is wrong and 2) I converted to float, but you can leave it as integer as string by doing 请注意,1)您的预期结果是错误的,并且2)我转换为float,但是您可以通过执行以下操作将其保留为整数作为字符串

result = {k:str(sum(v)//len(v)) for k,v in d.items()}

which gives: 这使:

{'PT': '67', 'FR': '25', 'UK': '33'}

Alternative solution using itertools.groupby() and itertools.tee() functions: 使用itertools.groupby()itertools.tee()函数的替代解决方案:

import itertools

l = [{'COUNTRY': 'UK', 'CREDITS': '54'}, {'COUNTRY': 'PT', 'CREDITS': '100'}, {'COUNTRY': 'FR', 'CREDITS': '20'}, {'COUNTRY': 'UK', 'CREDITS': '30'}, {'COUNTRY': 'UK', 'CREDITS': '15'}, {'COUNTRY': 'PT', 'CREDITS': '35'}, {'COUNTRY': 'FR', 'CREDITS': '30'}]
avgs = {}

for k,g in itertools.groupby(sorted(l, key=lambda x: x['COUNTRY']), key=lambda x: x['COUNTRY']):
    d1,d2 = itertools.tee(g)  # copy `grouper` iterator to deal with "fresh" pointer
    avgs[k] = sum(int(d['CREDITS']) for d in d1)/len(list(d2))

print(avgs)

The output: 输出:

{'UK': 33.0, 'FR': 25.0, 'PT': 67.5}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM