我有两个词典,需要将相似键的值组合在一起。这是一个例子:
dict1 = {'key1':[value11,value12,value13] , 'key2':[value21,value22,value23]}
dict2 = {'key1':[value14,value15] , 'key2':[value24,value25]}
我用过:
dict3 = {}
for key in (dict1.viewkeys() | dict2.keys()):
if key in dict1: dict3.setdefault(key, []).append(dict1[key])
if key in dict2: dict3.setdefault(key, []).append(dict2[key])
给了我:
dict3 = {'key1':[[value11,value12,value13],[value14,value15]] , 'key2':[[value21,value22,value23],[value24,value25]]}
我想要的是一个简单的:
期望的输出:
dict3 = {'key1':[value11,value12,value13,value14,value15] , 'key2':[value21,value22,value23,value24,value25]}
答案 0 :(得分:3)
您需要做的就是将append
修改为extend
,然后添加列表的元素而不是添加列表本身。有关append
和extend
之间差异的详细信息,请参阅list
docs。
dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
dict3 = {}
for key in set().union(dict1, dict2):
if key in dict1: dict3.setdefault(key, []).extend(dict1[key])
if key in dict2: dict3.setdefault(key, []).extend(dict2[key])
print(dict3)
# {'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}
或者,您可以使用默认设置为list
的{{3}},如下所示。
from collections import defaultdict
dict3 = defaultdict(list)
for key in set().union(dict1, dict2):
for dic in [dict1, dict2]:
if key in dic:
dict3[key] += dic[key]
答案 1 :(得分:2)
您可以更简单地做到这一点,但如果您想使用代码,只需将append
更改为extend
dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
dict3 = {}
for key in (dict1.viewkeys() | dict2.keys()):
if key in dict1: dict3.setdefault(key, []).extend(dict1[key])
if key in dict2: dict3.setdefault(key, []).extend(dict2[key])
print dict3
输出:
{'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}
你可以阅读这个post关于ov追加延伸的差异
答案 2 :(得分:1)
这是一个通用方法,您可以根据需要传递任意数量的dict。
>>> def mix_dict(*args):
res = {}
for d in args:
if not isinstance(d, dict):
continue
for k, v in d.iteritems():
res.setdefault(k, [])
if isinstance(v, list):
res[k].extend(v)
else:
res[k].append(v)
return res
>>> dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23']}
>>> dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
>>> dict3 = mix_dict(dict1, dict2)
>>> print dict3
... {'key1': ['value11', 'value12', 'value13', 'value14', 'value15'],
'key2': ['value21', 'value22', 'value23', 'value24', 'value25']}
答案 3 :(得分:1)
这是另一种方法。
您可以支持将列表的N个词组合并到具有此功能的列表的单个词典中:
def mergeDoLs(*dicts):
def flatten(LoL):
return [e for l in LoL for e in l]
rtr={k:[] for k in set(flatten(d.keys() for d in dicts))}
for k, v in flatten(d.items() for d in dicts):
rtr[k].extend(v)
return rtr
使用:
>>> dict1 = {'key1':['value11','value12','value13'] , 'key2':['value21','value22','value23'], 'key3':[1]}
>>> dict2 = {'key1':['value14','value15'] , 'key2':['value24','value25']}
>>> dict3 = {'key3':[2]}
>>> mergeDoLs(dict1, dict2, dict3)
{'key3': [1, 2], 'key2': ['value21', 'value22', 'value23', 'value24', 'value25'], 'key1': ['value11', 'value12', 'value13', 'value14', 'value15']}
答案 4 :(得分:0)
使用dict.update()合并两个字典键:property