I want to create finalDic that contains shared keys and the sum of their values
myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}, ...]
First find the common keys
commonkey = [{2:1, 3:1}, {2:3, 3:4}, {2:5, 3:6}]
Then we summarize and sort by their values
finalDic= {3:11, 2,9}
I tried this and donβt even close what I want
import collections myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}] def commonKey(x): i=0 allKeys = [] while i<len(x): for key in x[0].keys(): allKeys.append(key) i=i+1 commonKeys = collections.Counter(allKeys) commonKeys = [i for i in commonKeys if commonKeys[i]>len(x)-1] return commonKeys print commonKey(myDic)
thanks
source share