简体   繁体   English

在两个不同的词典中添加与相同键相关联的值

[英]Adding values associated with the same key in two different dictionaries

I've looked through a few of the questions here and none of them seem to be exactly my problem. 我在这里看了几个问题,但似乎没有一个问题。 Say I have 2 dictionaries, and they are dict1 说我有2个字典,它们是dict1

{'A': 25 , 'B': 41, 'C': 32}

and dict2 dict2

{'D':21, 'A': 12, 'B':62}

I'm writing a program where I need to add the values associated with the same key. 我正在编写一个程序,我需要添加与同一个键相关的值。 So like this: 像这样:

{'A': [25 + 12], 'B': [41 + 62], 'C': [32], 'D': [21]}

I am going to assume you are working to preserve dict_1 as the result 我将假设您正在努力保留dict_1作为结果

for key in dict_1:
    if key in dict_2:
        dict_1[key] = dict_1[key] + dict_2[key]

Need to copy key-value pairs that exist in 2 but not in 1 需要复制存在于2但不在1中的键值对

for key in dict_2:
    if key not in dict_1:
        dict_1[key] = dict_2[key]

Is probably the simplest way to do this 这可能是最简单的方法

>>> dict1= {'A':25,'B':41,'C':32}
>>> dict2= {'D':21, 'A':12, 'B':62}
>>> for e in dict2:
        if e in dict1:
            dict1[e]=dict1[e]+dict2[e]
        else:
            dict1[e]=dict2[e]

Now your dict1 becomes the answer you want. 现在你的dict1成为你想要的答案。 Hope that helps. 希望有所帮助。 :) :)

I think what you're missing in your solution is the use of the keys -method and the get -method that comes with dictionaries. 我认为你在解决方案中缺少的是使用keys -method和字典附带的get -method。 The keys -method will return a list of all of keys in the key-value pairs from your dictionary. keys -method将返回字典中键值对中所有键的列表。 Whereas, the get -method will return the value associated with a key in the dictionary, if it's there, or return a default (defaults to None ). 然而, get -method将返回与字典中的键相关联的值(如果存在),或返回默认值(默认为None )。

To do what you're trying to do, I'd recommend something like this: 为了做你想做的事,我建议这样的事情:

x = {'a': 1, 'b': 1, 'c': 1}              # dictionary x
y = {'c': 1, 'd': 1, 'e': 1}              # dictionary y
z = dict([
    (key, x.get(key, 0) + y.get(key, 0))  # key, sum(x,y)
    for key in                            # for every key in
    set(x.keys() + y.keys())              # the unique set of keys from x + y
])

Step #1: set(x.keys() + y.keys()) - add the two lists of keys together and removes the duplicates (by making it a set) 步骤#1: set(x.keys() + y.keys()) - 将两个键列表一起添加并删除重复项(通过使其成为一组)

Step #2: for key in - loop over every key in the set from Step #1 步骤2: for key in - 循环遍历步骤#1中集合中的每个键

Step #3: x.get(key, 0) + y.get(key, 0) - sum the values associated with that key from each of the dictionaries; 步骤#3: x.get(key, 0) + y.get(key, 0) - 对每个字典中与该键相关联的值求和; using 0 as a default if there is no associated value 如果没有关联值,则使用0作为默认值

Step #4: (key, ...) - create a key, value pair using the new sum and the original key 步骤#4 :( (key, ...) - 使用新的和和原始键创建一个键值对

Step #5: dict([...]) - create a new dictionary from the list of key-value pairs 步骤#5: dict([...]) - 从键值对列表中创建一个新词典


Does that make sense? 那有意义吗? I hope that helps! 我希望有所帮助!

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

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM