关于字典:如何在Python中将字典合并在一起?

How do I merge dictionaries together in Python?

1
d3 = dict(d1, **d2)

我知道这把字典合并了。但是,它是独一无二的吗?如果d1和d2键相同,但值不同怎么办?我希望将d1和d2合并,但如果有重复的键,d1具有优先权。


如果不再需要原来的d2,可以使用.update()方法:

Update the dictionary with the key/value pairs from other, overwriting existing keys. Return None.

例如。:

1
2
3
4
5
>>> d1 = {'a': 1, 'b': 2}
>>> d2 = {'b': 1, 'c': 3}
>>> d2.update(d1)
>>> d2
{'a': 1, 'c': 3, 'b': 2}

更新:

当然,为了创建新的合并词典,可以先复制词典。这可能是必要的,也可能不是必要的。如果您的字典中有复合对象(包含其他对象的对象,如列表或类实例),那么也应该考虑copy.deepcopy


在Python 2中,

1
2
d1={'a':1,'b':2}
d2={'a':10,'c':3}

d1覆盖d2:

1
2
dict(d2,**d1)
# {'a': 1, 'c': 3, 'b': 2}

d2覆盖d1:

1
2
dict(d1,**d2)
# {'a': 10, 'c': 3, 'b': 2}

这种行为不仅仅是一种实现的侥幸;它在文档中得到了保证:

If a key is specified both in the
positional argument and as a keyword
argument, the value associated with
the keyword is retained in the
dictionary.


如果您希望d1在冲突中具有优先权,请执行以下操作:

1
2
d3 = d2.copy()
d3.update(d1)

否则,反转d2d1


TreyHunner有一篇不错的博客文章,概述了合并多个字典的几种选择,包括(对于python3.3+)链图和字典解包。


我的解决方案是定义合并函数。它不复杂,只需要一条线。下面是python 3中的代码。

1
2
3
4
5
from functools import reduce
from operator import or_

def merge(*dicts):
    return { k: reduce(lambda d, x: x.get(k, d), dicts, None) for k in reduce(or_, map(lambda x: x.keys(), dicts), set()) }

测验

1
2
3
4
5
6
7
8
9
10
11
12
>>> d = {0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> d_letters = {0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 26: 'A', 27: 'B', 28: 'C', 29: 'D', 30: 'E', 31: 'F', 32: 'G', 33: 'H', 34: 'I', 35: 'J', 36: 'K', 37: 'L', 38: 'M', 39: 'N', 40: 'O', 41: 'P', 42: 'Q', 43: 'R', 44: 'S', 45: 'T', 46: 'U', 47: 'V', 48: 'W', 49: 'X', 50: 'Y', 51: 'Z'}
>>> merge(d, d_letters)
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 26: 'A', 27: 'B', 28: 'C', 29: 'D', 30: 'E', 31: 'F', 32: 'G', 33: 'H', 34: 'I', 35: 'J', 36: 'K', 37: 'L', 38: 'M', 39: 'N', 40: 'O', 41: 'P', 42: 'Q', 43: 'R', 44: 'S', 45: 'T', 46: 'U', 47: 'V', 48: 'W', 49: 'X', 50: 'Y', 51: 'Z'}
>>> merge(d_letters, d)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16, 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 26: 'A', 27: 'B', 28: 'C', 29: 'D', 30: 'E', 31: 'F', 32: 'G', 33: 'H', 34: 'I', 35: 'J', 36: 'K', 37: 'L', 38: 'M', 39: 'N', 40: 'O', 41: 'P', 42: 'Q', 43: 'R', 44: 'S', 45: 'T', 46: 'U', 47: 'V', 48: 'W', 49: 'X', 50: 'Y', 51: 'Z'}
>>> merge(d)
{0: 0, 1: 1, 2: 4, 3: 9, 4: 16}
>>> merge(d_letters)
{0: 'a', 1: 'b', 2: 'c', 3: 'd', 4: 'e', 5: 'f', 6: 'g', 7: 'h', 8: 'i', 9: 'j', 10: 'k', 11: 'l', 12: 'm', 13: 'n', 14: 'o', 15: 'p', 16: 'q', 17: 'r', 18: 's', 19: 't', 20: 'u', 21: 'v', 22: 'w', 23: 'x', 24: 'y', 25: 'z', 26: 'A', 27: 'B', 28: 'C', 29: 'D', 30: 'E', 31: 'F', 32: 'G', 33: 'H', 34: 'I', 35: 'J', 36: 'K', 37: 'L', 38: 'M', 39: 'N', 40: 'O', 41: 'P', 42: 'Q', 43: 'R', 44: 'S', 45: 'T', 46: 'U', 47: 'V', 48: 'W', 49: 'X', 50: 'Y', 51: 'Z'}
>>> merge()
{}

它适用于任意数量的字典参数。如果这些字典中有任何重复的键,参数列表中最右边的字典中的键将获胜。


我相信,如上所述,使用d2.update(d1)是最好的方法,如果您仍然需要,还可以先复制d2

不过,我想指出的是,由于关键字参数需要是字符串,所以通常情况下,dict(d1, **d2)实际上是合并字典的一种糟糕的方法,因此,如果有dict这样的代码,它将失败:

1
2
3
4
{
  1: 'foo',
  2: 'bar'
}