我想创建一个finalDic,它包含公共键及其值的和
myDic = [{2:1, 3:1, 5:2}, {3:4, 6:4, 2:3}, {2:5, 3:6}, ...]
首先找到公共密钥
^{pr2}$然后根据它们的值求和排序
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)
谢谢
只有一些提示:
作为一个练习,这个实现留给了OP。在
我是这样做的:
或者是疯狂的一句话:
^{pr2}$希望这有帮助。:)
相关问题 更多 >
编程相关推荐