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

5

我有一个字典列表:

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]}


你为什么要尝试这些疯狂的一行代码呢?使用简单的循环解决方案吧。 - Ashwini Chaudhary
3个回答

5

在这种情况下,使用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]})

@vaultah 你可以阅读源代码,找到这个链接 https://docs.python.org/2/library/functions.html#reduce - Mazdak
@vaultah 源代码没有问题 :) 但是使用 将二元函数累积应用于可迭代对象的项 不如使用像 defaultdictsetdefault 这样的线性解决方案高效! - Mazdak
1
@vaultah,reduce不是一个好主意,因为它在非关联或具有副作用的代码上使用反直觉,并且在避免副作用时算法慢。此外,比起简单循环,reduce更慢且不够清晰。 - Veedrac

3

虽然不是很高效,但相当漂亮的 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]}

1

不导入任何库

>>> 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]}

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接