def intersect(dicts):
common_keys = reduce(set.intersection, map(set, dicts))
num = float(len(dicts))
return {k: sum(d[k] for d in dicts)/num for k in common_keys}
Example:
>>> a = {'alpha':12,'beta':13,'gamma':14}
>>> b = {'andora':19,'beta':14,'gamma':19}
>>> c = {'beta':11,'gamma':20}
>>> intersect([a, b, c])
{'beta': 12.666666666666666, 'gamma': 17.666666666666668}
If you want to be able to call it like intersect(a, b, c)
instead of needing to wrap them in a list, just change the def
line to def intersect(*dicts)
(the body can stay exactly the same).