我想知道是否存在任何python函数来合并两个字典并合并具有公共键的所有值。
我发现函数可以追加两个字典,合并两个字典,但不合并其值。
示例:
D1 = [{k1: v01}, {k3: v03}, {k4: v04},}],
D2 = [{k1: v11}, {k2: v12}, {k4: v14},}],
这应该是预期的结果:
D3 = [
{k1: [v01, v11]},
{k2: [ v12]},
{K3: [v03 ]},
{k4: [v04, v14]},
]
答案 0 :(得分:1)
没有内置函数,但您可以为此使用defaultdict:
from collections import defaultdict
d = defaultdict(list)
for other in [d1, d1]:
for k, v in other.items():
d[k].append(v)
答案 1 :(得分:0)
不导入任何内容的解决方案:
# First initialize data, done correctly here.
D1 = [{'k1': 'v01'}, {'k3': 'v03'}, {'k4': 'v04'}]
D2 = [{'k1': 'v11'}, {'k2': 'v12'}, {'k4': 'v14'}]
# Get all unique keys
keys = {k for d in [*D1, *D2] for k in d}
# Initialize an empty dict
D3 = {x:[] for x in keys}
# sort to maintain order
D3 = dict(sorted(D3.items()))
#Iterate and extend
for x in [*D1, *D2]:
for k,v in x.items():
D3[k].append(v)
# NOTE: I do not recommend you convert a dictionary into a list of records.
# Nonetheless, here is how it would be done.
# To convert to a list
D3_list = [{k:v} for k,v in D3.items()]
print(D3_list)
# [{'k1': ['v01', 'v11']},
# {'k2': ['v12']},
# {'k3': ['v03']},
# {'k4': ['v04', 'v14']}]
答案 2 :(得分:0)
如果您打算使用实际的词典而不是词典列表,那么这会更容易。
D1 = dict(k1=1, k3=3, k4=4)
D2 = dict(k1=11, k2=12, k4=14)
没有简单的内置函数可以执行此操作,但是setdefault
方法很接近。
它尝试获取给定的密钥,但是如果不存在则创建它。
D3 = {}
for k, v in D1.items() | D2.items():
D3.setdefault(k, set()).add(v)
结果。
{'k4': {4, 14}, 'k1': {1, 11}, 'k3': {3}, 'k2': {12}}
这全部假设顺序无关紧要,只需组合组合即可。
答案 3 :(得分:0)
将字典合并在一起的更通用的方法可能如下所示。 (回答类似的 SO question)
def merge(combiner, dicts):
new_dict = {}
for d in dicts:
for k, v in d.items():
if k in new_dict:
new_dict[k] = combiner(new_dict[k], v)
else:
new_dict[k] = v
return new_dict
x = {'a': 'A', 'b': 'B'}
y = {'b': 'B', 'c': 'C'}
z = {'a': 'A', 'd': 'D'}
merge_dicts(combiner= lambda x, y: f'{x} AND {y}', dicts=(x,y,z))
# {'a': 'A AND A', 'b': 'B AND B', 'c': 'C', 'd': 'D'}