我一直在尝试创建一个将两个字典加在一起的函数,包括它们所持有的整数。例如:
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
如您所见,这些似乎都不起作用-就我所知,两个词典相互覆盖
有没有一种简洁地在一行或一个函数中执行此操作的方法?
答案 0 :(得分: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 :(得分:1)
您可以使用字典理解来添加两个字典中的元素:
{key: storage.get(key, 0) + shoppingBag[key] for key in shoppingBag}
输出
{'eggs': 10, 'chocolate': 200, 'cream cheese': 250, 'flour': 1200, 'rice': 1450}
答案 2 :(得分: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}
答案 3 :(得分:0)
我建议为此使用collections.Counter
。不要重新发明轮子。
FirstName Total
Isaac Frempong 2
Erick Ortiz 2
Dummy Data 2
Dummy Data 2
Dummy Data 2
Dummy Data 2
Dummy Data 2
答案 4 :(得分: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}
答案 5 :(得分: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)