Python 字典中的求和值

Python 字典中的求和值,python,python-3.x,dictionary,sum,Python,Python 3.x,Dictionary,Sum,我得到了这样的东西: > d1 = {'System tests': {'failed': 5, 'passed': 0, 'total': 5}, 'Func tests': {'failed': 5, 'passed': 0, 'total': 5}} > d2 = {'System tests': {'failed': 1, 'passed': 1, 'total': 2}, 'Func tests': {'failed': 3, 'passe

我得到了这样的东西:

> d1 = {'System tests': {'failed': 5, 'passed': 0, 'total': 5},
       'Func tests': {'failed': 5, 'passed': 0, 'total': 5}}

> d2 = {'System tests': {'failed': 1, 'passed': 1, 'total': 2}, 
        'Func tests': {'failed': 3, 'passed': 2, 'total': 5}}

> d3 = {'System tests': {'failed': 0, 'passed': 0, 'total': 0}, 
        'Func tests': {'failed': 1, 'passed': 0, 'total': 1}}
d4 = {'System tests': {'failed': 6, 'passed': 1, 'total': 7}, 
       'Func tests': {'failed': 9, 'passed': 2, 'total': 11}
我想将值“failed”和“passed”和“total”相加到一个字典中

所以输出应该是这样的:

> d1 = {'System tests': {'failed': 5, 'passed': 0, 'total': 5},
       'Func tests': {'failed': 5, 'passed': 0, 'total': 5}}

> d2 = {'System tests': {'failed': 1, 'passed': 1, 'total': 2}, 
        'Func tests': {'failed': 3, 'passed': 2, 'total': 5}}

> d3 = {'System tests': {'failed': 0, 'passed': 0, 'total': 0}, 
        'Func tests': {'failed': 1, 'passed': 0, 'total': 1}}
d4 = {'System tests': {'failed': 6, 'passed': 1, 'total': 7}, 
       'Func tests': {'failed': 9, 'passed': 2, 'total': 11}
做这件事最简单的方法是什么

我可以使用基本库,但集合除外。

解决方案必须是通用的,例如,如果将来会出现一些其他词典,则使用

代码: 测试代码: 结果:
defaultdict({
“系统测试”:计数器({'total':7,‘失败’:6,‘通过’:1}),
'Func tests':计数器({'total':11',failed':9',passed':2})
})
输入:

d1 = {'a': 100, 'b': 200, 'c':300}
d2 = {'a': 300, 'b': 200, 'd':400}
d = {k : d1.get(k, 0) + d2.get(k,0) for k in set(d1.keys()) | set(d2.keys())}
输出:

{'a': 400, 'b': 400, 'c': 300, 'd': 400}
{'System tests': {'failed': 6, 'total': 7, 'passed': 1}, 'Func tests': {'failed': 9, 'total': 11, 'passed': 2}}

这将为您提供所需的输出,而没有库

d4 = {}

for d in d1, d2, d3:
    for test, results in d.items():
        if test not in d4:
            d4[test] = {}
        for key, value in results.items():
            if key in d4[test]:
                d4[test][key] += value
            else:
                d4[test][key] = value
结果:

{'System tests': {'failed': 6, 'passed': 1, 'total': 7}, 'Func tests': {'failed': 9, 'passed': 2, 'total': 11}}

如果无法使用
集合计数器
,另一种解决方案是使用
减少

from functools import reduce # reduce was moved here in Python3

def add_dicts(dict_list):

    def add(fst, snd):
        return {k: fst[k] + snd[k] for k in fst}

    return reduce(add, dict_list[1:], dict_list[0])
在您的代码中使用它看起来是这样的

dict_list = [d1, d2, d3]

d4 = {}

for k in dict_list[0]:
    d4[k] = add_dicts([d[k] for d in dict_list])

尽管如此,这假设您的所有
dict
格式正确。

您可以将
zip
dict.items
一起使用:

d1 = {'System tests': {'failed': 5, 'passed': 0, 'total': 5},
   'Func tests': {'failed': 5, 'passed': 0, 'total': 5}}
d2 = {'System tests': {'failed': 1, 'passed': 1, 'total': 2}, 
    'Func tests': {'failed': 3, 'passed': 2, 'total': 5}}
d3 = {'System tests': {'failed': 0, 'passed': 0, 'total': 0}, 
    'Func tests': {'failed': 1, 'passed': 0, 'total': 1}}

def combine_dicts(*d):
   return {a:{i:sum(h[i] for h in [b, c, d]) for i in ['failed', 'passed', 'total']} for [a, b], [_, c], [_, d] in zip(*d)}

print(combine_dicts(d1.items(), d2.items(), d3.items()))
输出:

{'a': 400, 'b': 400, 'c': 300, 'd': 400}
{'System tests': {'failed': 6, 'total': 7, 'passed': 1}, 'Func tests': {'failed': 9, 'total': 11, 'passed': 2}}

欢迎来到stackoverflow!请拿起,仔细阅读,并提供一个复制您的问题。这正是我所需要的!