【问题标题】:How to merge N Python dictionaries without overwriting values?如何在不覆盖值的情况下合并 N 个 Python 字典?
【发布时间】:2015-04-23 20:30:18
【问题描述】:

我有这个字典列表:

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

【问题讨论】:

  • 你为什么要尝试这些疯狂的单衬里?寻找一个简单的基于循环的解决方案。

标签: python dictionary merge


【解决方案1】:

在这种情况下使用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 你可以阅读源码找到docs.python.org/2/library/functions.html#reduce
  • @vaultah 源代码没有问题 :) 但是当我们可以使用像 defaultdictsetdefault 这样的线性解决方案时,Apply function of two arguments cumulatively to the items of iterable 效率不高!
  • @vaultah Reduce 是一个坏主意,因为在非关联或副作用代码上使用它是违反直觉的,如果避免副作用,算法会很慢。此外,它比简单的循环更慢且不太清晰。
【解决方案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]}

【讨论】:

    【解决方案3】:

    不导入任何库

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2019-12-31
      • 1970-01-01
      • 2021-12-23
      • 2014-02-25
      • 1970-01-01
      • 2013-10-27
      • 2012-12-14
      相关资源
      最近更新 更多