Computing mean of all tuple values where 1st number is similar

北城以北 提交于 2019-12-11 02:29:08

问题


Consider list of tuples

[(7751, 0.9407466053962708), (6631, 0.03942129), (7751, 0.1235432)]

how to compute mean of all tuple values in pythonic way where 1st number is similar? for example the answer has to be

[(7751, 0.532144902698135), (6631, 0.03942129)]

回答1:


One way is using collections.defaultdict

from collections import defaultdict
lst = [(7751, 0.9407466053962708), (6631, 0.03942129), (7751, 0.1235432)]
d_dict = defaultdict(list)
for k,v in lst:
    d_dict[k].append(v)

[(k,sum(v)/len(v)) for k,v in d_dict.items()]
#[(7751, 0.5321449026981354), (6631, 0.03942129)]



回答2:


You do with groupby ,

from itertools import groupby
result = []
for i,g in groupby(sorted(lst),key=lambda x:x[0]):
    grp = list(g)
    result.append((i,sum(i[1] for i in grp)/len(grp)))

Using, list comprehension,

def get_avg(g):
    grp = list(g)
    return sum(i[1] for i in grp)/len(grp)

result = [(i,get_avg(g)) for i,g in groupby(sorted(lst),key=lambda x:x[0])]

Result

[(6631, 0.03942129), (7751, 0.5321449026981354)]



回答3:


groupby from itertools is your friend:

>>> l=[(7751, 0.9407466053962708), (6631, 0.03942129), (7751, 0.1235432)] 

>>> #importing libs:
>>> from itertools import groupby
>>> from statistics import mean              #(only python >= 3.4)
>>> # mean=lambda l: sum(l) / float(len(l))  #(for python < 3.4) (*1)

>>> #set the key to group and sort and sorting
>>> k=lambda x: x[0]         
>>> data = sorted(l, key=k)  

>>> #here it is, pythonic way:
>>> [ (k, mean([m[1] for m in g ])) for k, g in groupby(data, k) ] 

Results:

[(6631, 0.03942129), (7751, 0.5321449026981354)]

EDITED (*1) Thanks Elmex80s to refer me to mean.



来源:https://stackoverflow.com/questions/48007236/computing-mean-of-all-tuple-values-where-1st-number-is-similar

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!