简体   繁体   English

Python-在字典元素中查找平均值

[英]Python - Find average in dict elements

I have dict like: 我的字典像:

dict = [{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}]

I need to get average of all different keys. 我需要获取所有不同键的平均值。 Result should looks like: 结果应如下所示:

avg = [{'a':1.5, 'b':3.5, 'c':5}]

I can get summary of all keys, but Im failing to realize how can I count same keys in order to get average number. 我可以获取所有键的摘要,但是我无法意识到如何才能对相同的键进行计数以获取平均数。

This can be easily done with : 这可以通过轻松完成:

>>> import pandas
>>> df = pandas.DataFrame([{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}])
>>> df.mean()
a    1.5
b    3.5
c    5.0
dtype: float64

If you need a dictionary as result: 如果您需要字典作为结果:

>>> dict(df.mean())
{'a': 1.5, 'b': 3.5, 'c': 5.0}

You could create an intermediate dictionary that collects all encountered values as lists: 您可以创建一个中间字典来收集所有遇到的值作为列表:

dct = [{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}]
from collections import defaultdict
intermediate = defaultdict(list)

for subdict in dct:
    for key, value in subdict.items():
        intermediate[key].append(value)

# intermediate is now: defaultdict(list, {'a': [2, 1], 'b': [3, 4], 'c': [5]})

And finally calculate the average by dividing the sum of each list by the length of each list: 最后,通过将每个列表的总和除以每个列表的长度来计算平均值:

for key, value in intermediate.items():
    print(key, sum(value)/len(value))

which prints: 打印:

b 3.5
c 5.0
a 1.5

You can use a for loop with a counter and then divide the sum of each by the counter. 您可以将for循环用于计数器,然后将每个计数器的总和除以计数器。

Also it is weird you are calling the array/list a dict... 您将数组/列表称为字典也很奇怪...

I'd suggest something like this: 我建议这样的事情:

Create a new dict: letter_count = {} 创建一个新的字典:letter_count = {}

-For loop over the current dicts -For循环当前字典

-Add the letter to the letter count if it doesn't exist -如果字母不存在,则将其添加到字母数中

-If it does exist, update the value with the value of the item (+=number) as well as update the counter by one -如果确实存在,则使用项目的值(+ = number)更新值,并将计数器更新一

-Once the for loop is done, divide each value by the counter -for循环完成后,将每个值除以计数器

-Return the new dict letter_count -返回新的dict letter_count

I thought of adding a unique answer using PyFunctional 我想到了使用PyFunctional添加唯一答案

from functional import seq

l = [{'a':2, 'b':3}, {'b':4}, {'a':1, 'c':5}]
a = (seq(l)
     # convert dictionary to list
     .map(lambda d: seq(d).map(lambda k: (k, d[k])))
     .flatten()
     # append 1 for counter
     .map(lambda (k, v): (k, (v, 1)))
     # sum of values, and counts
     .reduce_by_key(lambda a, b: (a[0]+b[0], a[1]+b[1]))
     # average
     .map(lambda (k, (v, c)): (k, float(v)/c))
     # convert to dict
     .to_dict()
)
print(a)

Output 输出量

{'a': 1.5, 'c': 5.0, 'b': 3.5}

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

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