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
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.