如何将两个词典一起添加到Python中创建另一个带整数组件的字典?

问题描述 投票:0回答:6

我一直在努力创建一个将两个词典加在一起的函数,包括它们持有的整数。例如:

storage = {'flour': 200, 'eggs': 4, 'rice': 450}

shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

会成为:

{'flour': 1200, 'eggs': 10, 'rice': 1450, 'chocolate': 200, 'cream cheese', 250}

我尝试了几种方法:

storage = dict(storage, **shoppingBag)

#returns

{'flour': 1000, 'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'rice': 1000}

亚伦·霍尔的This method

def merge_two_dicts(x, y):
    z = x.copy()   
    z.update(y)
    return z

#returns

{'flour': 1000, 'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'rice': 1000}

还有这个

storage = dict(list(storage.items()) + list(shoppingBag.items()))

#returns

{'flour': 1000, 'eggs': 6, 'rice': 1000, 'chocolate': 200, 'cream cheese': 250}

#which is even worse

正如你所看到的,这些似乎都不起作用 - 据我所知,两个字典相互覆盖

有没有办法在一行或一个功能中简明扼要地做到这一点?

python dictionary
6个回答
1
投票

您可以使用字典理解来添加两个字典中的元素:

{key: storage.get(key, 0) + shoppingBag[key] for key in shoppingBag}

产量

{'eggs': 10, 'chocolate': 200, 'cream cheese': 250, 'flour': 1200, 'rice': 1450}

3
投票

使用collections.Counter

例如:

from collections import Counter

storage = {'flour': 200, 'eggs': 4, 'rice': 450}
shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

shoppingBag = Counter(shoppingBag) + Counter(storage)
print(shoppingBag)

输出:

Counter({'rice': 1450, 'flour': 1200, 'cream cheese': 250, 'chocolate': 200, 'eggs': 10})

1
投票

你可以这样做:

def merge_two_dicts(x, y):
    z = x.copy()
    for k, v in y.items():
        z[k] = z.get(k, 0) + v
    return z

storage = {'flour': 200, 'eggs': 4, 'rice': 450}
shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

print(merge_two_dicts(shoppingBag, storage))
# {'eggs': 10, 'chocolate': 200, 'cream cheese': 250, 'flour': 1200, 'rice': 1450}
print(merge_two_dicts(storage, shoppingBag))
# {'flour': 1200, 'eggs': 10, 'rice': 1450, 'chocolate': 200, 'cream cheese': 250}

0
投票

我建议使用collections.Counter。不要重新发明轮子。

storage = collections.Counter({'flour': 200, 'eggs': 4, 'rice': 450})
shoppingBag = collections.Counter({'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000})
result = storage + shoppingBag

0
投票
>>> new_dict1 = {key: storage.get(key, 0) + shoppingBag.get(key, 0) for key in set(shoppingBag) | set(storage)}
>>> new_dict1
{'eggs': 10, 'flour': 1200, 'rice': 1450, 'chocolate': 200, 'cream cheese': 250}

0
投票

下面的片段会帮助你!

方法1:

d1 = {'a': 100, 'b': 200, 'c': 300}
d2 = {'a': 300, 'b': 200, 'd': 400}

for key, value in d1.items():
    if key in d2:
        d1[key] = value + d2[key]
        del d2[key]
    z={**d1,**d2}

print(z)

方法2:

import copy

storage = {'flour': 200, 'eggs': 4, 'rice': 450}

shoppingBag = {'eggs': 6, 'chocolate': 200, 'cream cheese': 250, 'flour': 1000, 'rice': 1000}

def deep_copy(input_variable):
    return copy.deepcopy(input_variable)

def add_int_recursively(input_variable_1, input_variable_2):
    if input_variable_1 is None:
        return deep_copy(input_variable_2)
    elif input_variable_2 is None:
        return deep_copy(input_variable_1)
    elif isinstance(input_variable_1, int):
        return input_variable_1 + input_variable_2
    else:
        output_object = {}
        for key in set(input_variable_1) | set(input_variable_2):
            output_object[key] = add_int_recursively(input_variable_1.get(key), input_variable_2.get(key))
        return output_object

add_int_recursively(storage,shoppingBag)
© www.soinside.com 2019 - 2024. All rights reserved.