elegant way to reduce a list of dictionaries?

前端 未结 5 1239
我在风中等你
我在风中等你 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:22

    You could use a Counter to do the summing elegantly:

    from itertools import Counter
    
    summed = sum((Counter(d) for d in folds), Counter())
    averaged = {k: v/len(folds) for k, v in summed.items()}
    

    If you really feel like it, it can even be turned into a oneliner:

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

    In any case, I consider either more readable than a complicated reduce(); sum() itself is an appropriately specialized version of that.

    An even simpler oneliner that doesn't require any imports:

    averaged = {
        k: sum(d[k] for d in folds)/len(folds)
        for k in folds[0]
    }
    

    Interestingly, it's considerably faster (even than pandas?!), and also the statistic is easier to change.

    I tried replacing the manual calculation by statistics.mean() function in Python 3.5, but that makes it over 10 times slower.

提交回复
热议问题