如何合并N Python字典而不覆盖值?

时间:2015-04-23 20:30:18

标签: python dictionary merge

我有这个词典列表:

list_of_ds = [
    {'a': [1, 2], 'b': [4, 5], 'c': [6, 7]},
    {'a': [4], 'b': [56], 'c': [46]},
    {'a': [92], 'b': [65], 'c': [43]}
]

我想把它作为输出:

{'a': [1, 2, 4, 92], 'b': [4, 5, 56, 65], 'c': [6, 7, 46, 43]}

直到现在......

我试过

d_of_ds = reduce(lambda d1, d2: d1.update(d2), list_of_ds)

给出:AttributeError: 'NoneType' object has no attribute 'update'

我试过

d_of_ds = reduce(lambda d1, d2: d1.update(d2) or d1, list_of_ds, {})

覆盖每次迭代:{'a': [92], 'b': [65], 'c': [43]}

我试过

d_of_ds = {k: v for d in list_of_ds for k, v in d.items()}

覆盖每次迭代:{'a': [92], 'b': [65], 'c': [43]}

3 个答案:

答案 0 :(得分:4)

在这种情况下使用reduce并不是一个好主意。而且你的lambda函数根本就有错误的逻辑,因为你试图一起更新整个字典,而不是它们的元素看到以下内容:

>>> a={'a':[1,2], 'b':[4,5],'c':[6,7]}
>>> a.update({'a':[4], 'b':[56],'c':[46]})
>>> a
{'a': [4], 'c': [46], 'b': [56]}

但作为一种更有效的方法,您可以使用dict.setdefault方法:

>>> new={}
>>> for d in list_of_ds:
...    for i,j in d.items():
...       new.setdefault(i,[]).extend(j)
... 
>>> new
{'a': [1, 2, 4, 92], 'c': [6, 7, 46, 43], 'b': [4, 5, 56, 65]}

您也可以使用collections.defaultdict

>>> from collections import defaultdict
>>> d=defaultdict(list)
>>> for sub in list_of_ds:
...    for i,j in sub.items():
...       d[i].extend(j)
... 
>>> d
defaultdict(<type 'list'>, {'a': [1, 2, 4, 92], 'c': [6, 7, 46, 43], 'b': [4, 5, 56, 65]})

答案 1 :(得分:2)

效率不高但非常reduce解决方案:

def f(x, y):
    return {k: x.get(k, []) + y.get(k, []) for k in set(x).union(y)}

from functools import reduce
reduce(f, list_of_ds) # {'b': [4, 5, 56, 65], 'a': [1, 2, 4, 92], 'c': [6, 7, 46, 43]}

使用collections.defaultdict不是那么漂亮但有效的解决方案:

from collections import defaultdict

def f(list_of_ds):
    new = defaultdict(list)
    for d in list_of_ds:
        for k, v in d.items():
            new[k].extend(v)
    return new # or dict(new), to obtain a 'dict' object

f(list_of_ds) # {'a': [1, 2, 4, 92], 'b': [4, 5, 56, 65], 'c': [6, 7, 46, 43]}

答案 2 :(得分:0)

不导入任何库

>>> new_dict = {}
>>> new_dict
{}
>>> for dct in list_of_ds:
        for key,val in dct.items():
            if key not in new_dict.keys(): new_dict[key] = val
            else: new_dict[key].extend(val)


>>> new_dict
{'a': [1, 2, 4, 92], 'c': [6, 7, 46, 43], 'b': [4, 5, 56, 65]}
相关问题