我想创建一个包含公共键和它们的值总和的 finalDic
myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}, ...]
首先找到常用键
commonkey = [{2:1, 3:1}, {2:3, 3:4}, {2:5, 3:6}]
然后求和并按它们的值排序
finalDic= {3:11, 2,9}
我已经尝试过了,甚至没有关闭我想要的
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)
谢谢