简体   繁体   English

如何在python中的不同字典中添加相同键的值?

[英]How can I add the values of the same keys in different dictionaries in python?

I have something like this:我有这样的事情:

A = {"Green":{"Small": 5, "Medium": 10, "Large": 15}, "Yellow": {"Small": 7, "Medium": 14, "Large": 21}}

B = {"Green":{"Small": 1, "Medium": 2, "Large": 3}, "Yellow": {"Small": 3, "Medium": 6, "Large": 9}}

I want to write a function to get a dictionary "C" like this:我想编写一个函数来获取这样的字典“C”:

C = {"Green":{"Small": 6, "Medium": 12, "Large": 18}, "Yellow": {"Small": 10, "Medium": 20, "Large": 30}}

Which is just the sum of each value for it's key (which is the same in all dictionaries. Right now my "solution" is way too long and certainly not elegant. Can anyone give me some pointers on how I can make a relatively short function to achieve this?这只是它的键的每个值的总和(在所有字典中都是相同的。现在我的“解决方案”太长而且肯定不优雅。谁能给我一些关于如何制作相对较短函数的指示为达到这个?

Good old loops:好的旧循环:

C = {}
for k, v in A.items():
    inner = {}
    for inner_k, inner_v in v.items():
        inner[inner_k] = inner_v + B[k][inner_k]
    C[k] = inner

>>> C
{'Green': {'Large': 18, 'Medium': 12, 'Small': 6},
 'Yellow': {'Large': 30, 'Medium': 20, 'Small': 10}}

A function which takes an arbitrary number of dictionaries in the form OP describes:一个采用 OP 形式的任意数量的字典的函数描述:

from pprint import pprint

def dsum(*args):
    return {
        k1: {
            k: sum(d.get(k1, dict()).get(k, 0) for d in args)
            for k in set.union(*(set(d.get(k1, dict())) for d in args))
        }
        for k1 in set.union(*(set(d) for d in args))
    }

A = {"Green":{"Small": 5, "Medium": 10, "Large": 15}, "Yellow": {"Small": 7, "Medium": 14, "Large": 21}}

B = {"Green":{"Small": 1, "Medium": 2, "Large": 3}, "Yellow": {"Small": 3, "Medium": 6, "Large": 9}}

C = {"Green":{"Small": 6, "Medium": 12, "Large": 18}, "Yellow": {"Small": 10, "Medium": 20, "Large": 30}}

testC = dsum(A, B)
assert C == testC
pprint(testC)
C={k: {kk: v + B[k][kk] for kk, v in a.items()} for k, a in A.items()}

在 python2 中更喜欢.iteritems()

A solution for missing keys:丢失密钥的解决方案:

def merge_by_addition(d1, d2):
    def m2(d1_, d2_):
        keys_ = set(d1_.keys()) | set(d2_.keys())
        return {k_: (d1_.get(k_, 0) + d2_.get(k_, 0)) for k_ in keys_}
    keys = set(d1.keys()) | set(d2.keys())
    return {k: m2(d1.get(k, {}), d2.get(k, {})) for k in keys}

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何将相同的值添加到字典中的不同键? - How to add same values to different keys in dictionaries? 如何在 JSON 文件中更改多个键 - How can I change multiple keys in a JSON file that has multiple dictionaries which incorporate the same keys but different values with Python 在Python中将2个具有相同键但值不同的字典组合在一起 - Combine 2 dictionaries with the same keys but different values in Python 使用相同键但值不同的Python字典 - Python dictionaries using same keys but different values 如何在python中的不同字典中存储相同的键? - how to store same keys in different dictionaries in python? 我想从2个python字典构造一个列表,这些字典具有相同的键,但键的值不同 - I want to construct a single list out of 2 python dictionaries which have same keys but different values for the keys 如何将具有多个文本列表值的python字典拆分为具有相同值的键的单独字典? - How can I split a python dictionary with multiple text-list values to have separate dictionaries of keys that have the same values? 如何将字典与相同的键结合起来? - How can I combine dictionaries with the same keys? Python:总结具有不同键和相同值的字典列表 - Python: summing up list of dictionaries with different keys and same values 比较四个字典,它们的键相同但每个字典在 python 中都有不同的值 - comparing four dictionaries that their keys are same but each has different values in python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM