elegant way to reduce a list of dictionaries?

前端 未结 5 1211
我在风中等你
我在风中等你 2021-01-13 02:30

I have a list of dictionaries and each dictionary contains exactly the same keys. I want to find the average value for each key and I would like to know how to do it using r

5条回答
  •  说谎
    说谎 (楼主)
    2021-01-13 03:05

    Here is a terrible one liner using list comprehension. You probably are better off not using this.

    final =  dict(zip(lst[0].keys(), [n/len(lst) for n in [sum(i) for i in zip(*[tuple(x1.values()) for x1 in lst])]]))
    
    for key, value in final.items():
        print (key, value)
    
    #Output
    recall 0.818703703704
    precision 0.820167289406
    f_measure 0.818495375556
    accuracy 0.79
    

提交回复
热议问题