问题:如何在Python中的单个表达式中合并两个字典?
我有两个Python字典,我想编写一个返回合并的这两个字典的表达式。update()
如果返回结果而不是就地修改字典,该方法将是我所需要的。
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = x.update(y)
>>> print(z)
None
>>> x
{'a': 1, 'b': 10, 'c': 11}
我怎样才能在最终的合并字典z
,不是x
?
(更清楚地说,dict.update()
我正在寻找的最后一个胜出的冲突处理方法是。)
I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged. The update()
method would be what I need, if it returned its result instead of modifying a dictionary in-place.
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 10, 'c': 11}
>>> z = x.update(y)
>>> print(z)
None
>>> x
{'a': 1, 'b': 10, 'c': 11}
How can I get that final merged dictionary in z
, not x
?
(To be extra-clear, the last-one-wins conflict-handling of dict.update()
is what I’m looking for as well.)
回答 0
如何在一个表达式中合并两个Python字典?
对于字典x
和y
,z
变成了浅层合并的字典,带有y
替换的值x
。
在Python 3.5或更高版本中:
z = {**x, **y}
在Python 2(或3.4或更低版本)中,编写一个函数:
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
现在:
z = merge_two_dicts(x, y)
在Python 3.9.0a4以上(最终发布日期大约2020年10月):PEP-584,这里讨论,执行,以进一步简化这一点:
z = x | y # NOTE: 3.9+ ONLY
说明
假设您有两个字典,并且想要将它们合并为新字典而不更改原始字典:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
理想的结果是获得一个z
合并了值的新字典(),第二个dict的值覆盖第一个字典的值。
>>> z
{'a': 1, 'b': 3, 'c': 4}
在PEP 448中提出并从Python 3.5开始可用的新语法是
z = {**x, **y}
它确实是一个表达。
注意,我们也可以使用文字符号合并:
z = {**x, 'foo': 1, 'bar': 2, **y}
现在:
>>> z
{'a': 1, 'b': 3, 'foo': 1, 'bar': 2, 'c': 4}
它现在显示为在3.5的发布时间表中实现,PEP 478,并且已进入Python 3.5的新功能文档。
但是,由于许多组织仍在使用Python 2,因此您可能希望以向后兼容的方式进行操作。在Python 2和Python 3.0-3.4中可用的经典Pythonic方法是分两个步骤完成的:
z = x.copy()
z.update(y) # which returns None since it mutates z
在这两种方法中,y
将排第二,其值将替换x
的值,因此'b'
将指向3
我们的最终结果。
尚未在Python 3.5上运行,但需要一个表达式
如果您尚未使用Python 3.5,或者需要编写向后兼容的代码,并且希望在单个表达式中使用它,则最有效的方法是将其放入函数中:
def merge_two_dicts(x, y):
"""Given two dicts, merge them into a new dict as a shallow copy."""
z = x.copy()
z.update(y)
return z
然后您有一个表达式:
z = merge_two_dicts(x, y)
您还可以创建一个函数来合并未定义数量的dict,从零到非常大的数量:
def merge_dicts(*dict_args):
"""
Given any number of dicts, shallow copy and merge into a new dict,
precedence goes to key value pairs in latter dicts.
"""
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
此函数将在Python 2和3中适用于所有字典。例如给以下a
命令g
:
z = merge_dicts(a, b, c, d, e, f, g)
和中的键值对g
优先a
于f
,以此类推。
其他答案的批判
不要使用以前接受的答案中看到的内容:
z = dict(x.items() + y.items())
在Python 2中,您将在每个内存字典中创建两个列表,在内存中创建第三个列表,其长度等于前两个字典的长度,然后丢弃所有三个列表以创建字典。在Python 3中,这将失败,因为您将两个dict_items
对象而不是两个列表加在一起-
>>> c = dict(a.items() + b.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
并且您必须将它们明确创建为列表,例如z = dict(list(x.items()) + list(y.items()))
。这浪费了资源和计算能力。
类似地,当值是不可散列的对象(例如列表)时,items()
在Python 3(viewitems()
在Python 2.7中)进行联合也将失败。即使您的值是可哈希的,由于集合在语义上是无序的,因此关于优先级的行为是不确定的。所以不要这样做:
>>> c = dict(a.items() | b.items())
此示例演示了值不可散列时会发生的情况:
>>> x = {'a': []}
>>> y = {'b': []}
>>> dict(x.items() | y.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
这是一个示例,其中y应该优先,但是由于集合的任意顺序,保留了x的值:
>>> x = {'a': 2}
>>> y = {'a': 1}
>>> dict(x.items() | y.items())
{'a': 2}
您不应该使用的另一种技巧:
z = dict(x, **y)
这使用了dict
构造函数,并且非常快速且具有内存效率(甚至比我们的两步过程略高),但是除非您确切地知道这里正在发生什么(也就是说,第二个dict作为关键字参数传递给dict,构造函数),很难阅读,它不是预期的用法,因此不是Pythonic。
这是在django中修复的用法示例。
字典旨在获取可散列的键(例如,frozenset或元组),但是当键不是字符串时,此方法在Python 3中失败。
>>> c = dict(a, **b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: keyword arguments must be strings
在邮件列表中,该语言的创建者Guido van Rossum写道:
我宣布dict({},** {1:3})非法是可以的,因为毕竟这是对**机制的滥用。
和
显然dict(x,** y)被“调用x.update(y)并返回x”的“酷砍”。我个人觉得它比酷更卑鄙。
根据我的理解(以及对语言创建者的理解),该命令的预期用途dict(**y)
是用于创建可读性强的命令,例如:
dict(a=1, b=10, c=11)
代替
{'a': 1, 'b': 10, 'c': 11}
对评论的回应
尽管Guido说了什么dict(x, **y)
,但符合dict规范,顺便说一句。它仅适用于Python 2和3。事实上,这仅适用于字符串键,这是关键字参数如何工作的直接结果,而不是字典的简称。在这个地方使用**运算符也不会滥用该机制,实际上**正是为了将dict作为关键字传递而设计的。
同样,当键为非字符串时,它不适用于3。隐式调用协定是命名空间采用普通命令,而用户只能传递字符串形式的关键字参数。所有其他可调用对象都强制执行了它。dict
在Python 2中破坏了这种一致性:
>>> foo(**{('a', 'b'): None})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() keywords must be strings
>>> dict(**{('a', 'b'): None})
{('a', 'b'): None}
考虑到其他Python实现(Pypy,Jython,IronPython),这种不一致是很糟糕的。因此,它在Python 3中已得到修复,因为这种用法可能是一个重大更改。
我向您指出,故意编写仅在一种语言版本中有效或仅在特定的任意约束下有效的代码是恶意的无能。
更多评论:
dict(x.items() + y.items())
仍然是Python 2最具可读性的解决方案。可读性至关重要。
我的回答:merge_two_dicts(x, y)
如果我们实际上担心可读性,实际上对我来说似乎更加清晰。而且它不向前兼容,因为Python 2越来越不推荐使用。
{**x, **y}
似乎不处理嵌套字典。嵌套键的内容只是被覆盖,而不是被合并。我最终被这些没有递归合并的答案所烧死,令我惊讶的是,没有人提到它。在我对“合并”一词的解释中,这些答案描述的是“将一个词典与另一个词典更新”,而不是合并。
是。我必须回头再问这个问题,它要求两个字典进行浅层合并,第一个字典的值由第二个字典覆盖-在一个表达式中。
假设有两个字典,一个字典可能会递归地将它们合并到一个函数中,但是您应注意不要从任何一个源修改字典,避免这种情况的最可靠方法是在分配值时进行复制。由于键必须是可散列的,因此通常是不可变的,因此复制它们毫无意义:
from copy import deepcopy
def dict_of_dicts_merge(x, y):
z = {}
overlapping_keys = x.keys() & y.keys()
for key in overlapping_keys:
z[key] = dict_of_dicts_merge(x[key], y[key])
for key in x.keys() - overlapping_keys:
z[key] = deepcopy(x[key])
for key in y.keys() - overlapping_keys:
z[key] = deepcopy(y[key])
return z
用法:
>>> x = {'a':{1:{}}, 'b': {2:{}}}
>>> y = {'b':{10:{}}, 'c': {11:{}}}
>>> dict_of_dicts_merge(x, y)
{'b': {2: {}, 10: {}}, 'a': {1: {}}, 'c': {11: {}}}
提出其他值类型的偶发性问题远远超出了此问题的范围,因此,我将为您回答有关“词典合并字典”的规范问题的答案。
性能较差但临时性正确
这些方法的性能较差,但是它们将提供正确的行为。他们将少得多比高性能copy
和update
或新的拆包,因为他们通过在更高的抽象水平的每个键-值对迭代,但他们做的尊重优先顺序(后者类型的字典具有优先权)
您还可以在dict理解内手动将dict链接:
{k: v for d in dicts for k, v in d.items()} # iteritems in Python 2.7
或在python 2.6中(也许在引入生成器表达式时早在2.4中):
dict((k, v) for d in dicts for k, v in d.items())
itertools.chain
将以正确的顺序在键值对上链接迭代器:
import itertools
z = dict(itertools.chain(x.iteritems(), y.iteritems()))
绩效分析
我将仅对已知行为正确的用法进行性能分析。
import timeit
在Ubuntu 14.04上完成以下操作
在Python 2.7(系统Python)中:
>>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
0.5726828575134277
>>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
1.163769006729126
>>> min(timeit.repeat(lambda: dict(itertools.chain(x.iteritems(), y.iteritems()))))
1.1614501476287842
>>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
2.2345519065856934
在Python 3.5中(死神PPA):
>>> min(timeit.repeat(lambda: {**x, **y}))
0.4094954460160807
>>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
0.7881555100320838
>>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
1.4525277839857154
>>> min(timeit.repeat(lambda: dict(itertools.chain(x.items(), y.items()))))
2.3143140770262107
>>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
3.2069112799945287
词典资源
How can I merge two Python dictionaries in a single expression?
For dictionaries x
and y
, z
becomes a shallowly merged dictionary with values from y
replacing those from x
.
In Python 3.5 or greater:
z = {**x, **y}
In Python 2, (or 3.4 or lower) write a function:
def merge_two_dicts(x, y):
z = x.copy() # start with x's keys and values
z.update(y) # modifies z with y's keys and values & returns None
return z
and now:
z = merge_two_dicts(x, y)
In Python 3.9.0a4 or greater (final release date approx October 2020): PEP-584, discussed here, was implemented to further simplify this:
z = x | y # NOTE: 3.9+ ONLY
Explanation
Say you have two dicts and you want to merge them into a new dict without altering the original dicts:
x = {'a': 1, 'b': 2}
y = {'b': 3, 'c': 4}
The desired result is to get a new dictionary (z
) with the values merged, and the second dict’s values overwriting those from the first.
>>> z
{'a': 1, 'b': 3, 'c': 4}
A new syntax for this, proposed in PEP 448 and available as of Python 3.5, is
z = {**x, **y}
And it is indeed a single expression.
Note that we can merge in with literal notation as well:
z = {**x, 'foo': 1, 'bar': 2, **y}
and now:
>>> z
{'a': 1, 'b': 3, 'foo': 1, 'bar': 2, 'c': 4}
It is now showing as implemented in the release schedule for 3.5, PEP 478, and it has now made its way into What’s New in Python 3.5 document.
However, since many organizations are still on Python 2, you may wish to do this in a backwards compatible way. The classically Pythonic way, available in Python 2 and Python 3.0-3.4, is to do this as a two-step process:
z = x.copy()
z.update(y) # which returns None since it mutates z
In both approaches, y
will come second and its values will replace x
‘s values, thus 'b'
will point to 3
in our final result.
Not yet on Python 3.5, but want a single expression
If you are not yet on Python 3.5, or need to write backward-compatible code, and you want this in a single expression, the most performant while correct approach is to put it in a function:
def merge_two_dicts(x, y):
"""Given two dicts, merge them into a new dict as a shallow copy."""
z = x.copy()
z.update(y)
return z
and then you have a single expression:
z = merge_two_dicts(x, y)
You can also make a function to merge an undefined number of dicts, from zero to a very large number:
def merge_dicts(*dict_args):
"""
Given any number of dicts, shallow copy and merge into a new dict,
precedence goes to key value pairs in latter dicts.
"""
result = {}
for dictionary in dict_args:
result.update(dictionary)
return result
This function will work in Python 2 and 3 for all dicts. e.g. given dicts a
to g
:
z = merge_dicts(a, b, c, d, e, f, g)
and key value pairs in g
will take precedence over dicts a
to f
, and so on.
Critiques of Other Answers
Don’t use what you see in the formerly accepted answer:
z = dict(x.items() + y.items())
In Python 2, you create two lists in memory for each dict, create a third list in memory with length equal to the length of the first two put together, and then discard all three lists to create the dict. In Python 3, this will fail because you’re adding two dict_items
objects together, not two lists –
>>> c = dict(a.items() + b.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unsupported operand type(s) for +: 'dict_items' and 'dict_items'
and you would have to explicitly create them as lists, e.g. z = dict(list(x.items()) + list(y.items()))
. This is a waste of resources and computation power.
Similarly, taking the union of items()
in Python 3 (viewitems()
in Python 2.7) will also fail when values are unhashable objects (like lists, for example). Even if your values are hashable, since sets are semantically unordered, the behavior is undefined in regards to precedence. So don’t do this:
>>> c = dict(a.items() | b.items())
This example demonstrates what happens when values are unhashable:
>>> x = {'a': []}
>>> y = {'b': []}
>>> dict(x.items() | y.items())
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: unhashable type: 'list'
Here’s an example where y should have precedence, but instead the value from x is retained due to the arbitrary order of sets:
>>> x = {'a': 2}
>>> y = {'a': 1}
>>> dict(x.items() | y.items())
{'a': 2}
Another hack you should not use:
z = dict(x, **y)
This uses the dict
constructor, and is very fast and memory efficient (even slightly more-so than our two-step process) but unless you know precisely what is happening here (that is, the second dict is being passed as keyword arguments to the dict constructor), it’s difficult to read, it’s not the intended usage, and so it is not Pythonic.
Here’s an example of the usage being remediated in django.
Dicts are intended to take hashable keys (e.g. frozensets or tuples), but this method fails in Python 3 when keys are not strings.
>>> c = dict(a, **b)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: keyword arguments must be strings
From the mailing list, Guido van Rossum, the creator of the language, wrote:
I am fine with
declaring dict({}, **{1:3}) illegal, since after all it is abuse of
the ** mechanism.
and
Apparently dict(x, **y) is going around as “cool hack” for “call
x.update(y) and return x”. Personally I find it more despicable than
cool.
It is my understanding (as well as the understanding of the creator of the language) that the intended usage for dict(**y)
is for creating dicts for readability purposes, e.g.:
dict(a=1, b=10, c=11)
instead of
{'a': 1, 'b': 10, 'c': 11}
Despite what Guido says, dict(x, **y)
is in line with the dict specification, which btw. works for both Python 2 and 3. The fact that this only works for string keys is a direct consequence of how keyword parameters work and not a short-comming of dict. Nor is using the ** operator in this place an abuse of the mechanism, in fact ** was designed precisely to pass dicts as keywords.
Again, it doesn’t work for 3 when keys are non-strings. The implicit calling contract is that namespaces take ordinary dicts, while users must only pass keyword arguments that are strings. All other callables enforced it. dict
broke this consistency in Python 2:
>>> foo(**{('a', 'b'): None})
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: foo() keywords must be strings
>>> dict(**{('a', 'b'): None})
{('a', 'b'): None}
This inconsistency was bad given other implementations of Python (Pypy, Jython, IronPython). Thus it was fixed in Python 3, as this usage could be a breaking change.
I submit to you that it is malicious incompetence to intentionally write code that only works in one version of a language or that only works given certain arbitrary constraints.
More comments:
dict(x.items() + y.items())
is still the most readable solution for Python 2. Readability counts.
My response: merge_two_dicts(x, y)
actually seems much clearer to me, if we’re actually concerned about readability. And it is not forward compatible, as Python 2 is increasingly deprecated.
{**x, **y}
does not seem to handle nested dictionaries. the contents of nested keys are simply overwritten, not merged […] I ended up being burnt by these answers that do not merge recursively and I was surprised no one mentioned it. In my interpretation of the word “merging” these answers describe “updating one dict with another”, and not merging.
Yes. I must refer you back to the question, which is asking for a shallow merge of two dictionaries, with the first’s values being overwritten by the second’s – in a single expression.
Assuming two dictionary of dictionaries, one might recursively merge them in a single function, but you should be careful not to modify the dicts from either source, and the surest way to avoid that is to make a copy when assigning values. As keys must be hashable and are usually therefore immutable, it is pointless to copy them:
from copy import deepcopy
def dict_of_dicts_merge(x, y):
z = {}
overlapping_keys = x.keys() & y.keys()
for key in overlapping_keys:
z[key] = dict_of_dicts_merge(x[key], y[key])
for key in x.keys() - overlapping_keys:
z[key] = deepcopy(x[key])
for key in y.keys() - overlapping_keys:
z[key] = deepcopy(y[key])
return z
Usage:
>>> x = {'a':{1:{}}, 'b': {2:{}}}
>>> y = {'b':{10:{}}, 'c': {11:{}}}
>>> dict_of_dicts_merge(x, y)
{'b': {2: {}, 10: {}}, 'a': {1: {}}, 'c': {11: {}}}
Coming up with contingencies for other value types is far beyond the scope of this question, so I will point you at my answer to the canonical question on a “Dictionaries of dictionaries merge”.
Less Performant But Correct Ad-hocs
These approaches are less performant, but they will provide correct behavior.
They will be much less performant than copy
and update
or the new unpacking because they iterate through each key-value pair at a higher level of abstraction, but they do respect the order of precedence (latter dicts have precedence)
You can also chain the dicts manually inside a dict comprehension:
{k: v for d in dicts for k, v in d.items()} # iteritems in Python 2.7
or in python 2.6 (and perhaps as early as 2.4 when generator expressions were introduced):
dict((k, v) for d in dicts for k, v in d.items())
itertools.chain
will chain the iterators over the key-value pairs in the correct order:
import itertools
z = dict(itertools.chain(x.iteritems(), y.iteritems()))
Performance Analysis
I’m only going to do the performance analysis of the usages known to behave correctly.
import timeit
The following is done on Ubuntu 14.04
In Python 2.7 (system Python):
>>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
0.5726828575134277
>>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
1.163769006729126
>>> min(timeit.repeat(lambda: dict(itertools.chain(x.iteritems(), y.iteritems()))))
1.1614501476287842
>>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
2.2345519065856934
In Python 3.5 (deadsnakes PPA):
>>> min(timeit.repeat(lambda: {**x, **y}))
0.4094954460160807
>>> min(timeit.repeat(lambda: merge_two_dicts(x, y)))
0.7881555100320838
>>> min(timeit.repeat(lambda: {k: v for d in (x, y) for k, v in d.items()} ))
1.4525277839857154
>>> min(timeit.repeat(lambda: dict(itertools.chain(x.items(), y.items()))))
2.3143140770262107
>>> min(timeit.repeat(lambda: dict((k, v) for d in (x, y) for k, v in d.items())))
3.2069112799945287
Resources on Dictionaries
回答 1
您的情况是:
z = dict(x.items() + y.items())
可以根据需要将最终的dict放入中z
,并使key的值b
被第二(y
)dict的值正确覆盖:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = dict(x.items() + y.items())
>>> z
{'a': 1, 'c': 11, 'b': 10}
如果您使用Python 3,它只会稍微复杂一点。创建z
:
>>> z = dict(list(x.items()) + list(y.items()))
>>> z
{'a': 1, 'c': 11, 'b': 10}
如果您使用Python版本3.9.0a4或更高版本,则可以直接使用:
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = x | y
print(z)
Output: {'a': 1, 'c': 11, 'b': 10}
In your case, what you can do is:
z = dict(x.items() + y.items())
This will, as you want it, put the final dict in z
, and make the value for key b
be properly overridden by the second (y
) dict’s value:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = dict(x.items() + y.items())
>>> z
{'a': 1, 'c': 11, 'b': 10}
If you use Python 3, it is only a little more complicated. To create z
:
>>> z = dict(list(x.items()) + list(y.items()))
>>> z
{'a': 1, 'c': 11, 'b': 10}
If you use Python version 3.9.0a4 or greater, then you can directly use:
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = x | y
print(z)
Output: {'a': 1, 'c': 11, 'b': 10}
回答 2
替代:
z = x.copy()
z.update(y)
An alternative:
z = x.copy()
z.update(y)
回答 3
另一个更简洁的选择:
z = dict(x, **y)
注意:这已经成为一个流行的答案,但必须指出的是,如果y
有任何非字符串键,那么它实际上是对CPython实现细节的滥用,并且在Python 3中不起作用,或在PyPy,IronPython或Jython中。另外,Guido也不是粉丝。因此,我不建议将此技术用于前向兼容或交叉实现的可移植代码,这实际上意味着应完全避免使用它。
Another, more concise, option:
z = dict(x, **y)
Note: this has become a popular answer, but it is important to point out that if y
has any non-string keys, the fact that this works at all is an abuse of a CPython implementation detail, and it does not work in Python 3, or in PyPy, IronPython, or Jython. Also, Guido is not a fan. So I can’t recommend this technique for forward-compatible or cross-implementation portable code, which really means it should be avoided entirely.
回答 4
这可能不是一个流行的答案,但是您几乎可以肯定不想这样做。如果要合并的副本,请使用copy(或deepcopy,具体取决于您的需求),然后进行更新。与使用.items()+ .items()进行单行创建相比,两行代码更具可读性-更具Python风格。显式胜于隐式。
此外,当您使用.items()(Python 3.0之前的版本)时,您正在创建一个新列表,其中包含字典中的项目。如果您的词典很大,那将是很大的开销(创建合并字典后将立即丢弃两个大列表)。update()可以更高效地工作,因为它可以逐项执行第二个字典。
在时间方面:
>>> timeit.Timer("dict(x, **y)", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
15.52571702003479
>>> timeit.Timer("temp = x.copy()\ntemp.update(y)", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
15.694622993469238
>>> timeit.Timer("dict(x.items() + y.items())", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
41.484580039978027
IMO出于可读性考虑,前两者之间的微小速度下降是值得的。此外,仅在Python 2.3中添加了用于字典创建的关键字参数,而copy()和update()将在较早的版本中运行。
This probably won’t be a popular answer, but you almost certainly do not want to do this. If you want a copy that’s a merge, then use copy (or deepcopy, depending on what you want) and then update. The two lines of code are much more readable – more Pythonic – than the single line creation with .items() + .items(). Explicit is better than implicit.
In addition, when you use .items() (pre Python 3.0), you’re creating a new list that contains the items from the dict. If your dictionaries are large, then that is quite a lot of overhead (two large lists that will be thrown away as soon as the merged dict is created). update() can work more efficiently, because it can run through the second dict item-by-item.
In terms of time:
>>> timeit.Timer("dict(x, **y)", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
15.52571702003479
>>> timeit.Timer("temp = x.copy()\ntemp.update(y)", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
15.694622993469238
>>> timeit.Timer("dict(x.items() + y.items())", "x = dict(zip(range(1000), range(1000)))\ny=dict(zip(range(1000,2000), range(1000,2000)))").timeit(100000)
41.484580039978027
IMO the tiny slowdown between the first two is worth it for the readability. In addition, keyword arguments for dictionary creation was only added in Python 2.3, whereas copy() and update() will work in older versions.
回答 5
在后续回答中,您询问了这两种选择的相对性能:
z1 = dict(x.items() + y.items())
z2 = dict(x, **y)
至少在我的机器上(运行Python 2.5.2的相当普通的x86_64),替代z2
方法不仅更短,更简单,而且显着更快。您可以使用timeit
Python随附的模块自行验证。
示例1:相同的字典将20个连续的整数映射到自身:
% python -m timeit -s 'x=y=dict((i,i) for i in range(20))' 'z1=dict(x.items() + y.items())'
100000 loops, best of 3: 5.67 usec per loop
% python -m timeit -s 'x=y=dict((i,i) for i in range(20))' 'z2=dict(x, **y)'
100000 loops, best of 3: 1.53 usec per loop
z2
胜出率约为3.5。不同的词典似乎会产生完全不同的结果,但z2
似乎总是遥遥领先。(如果同一测试的结果不一致,请尝试传递-r
大于默认值3的数字。)
示例2:非重叠字典将252个短字符串映射为整数,反之亦然:
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z1=dict(x.items() + y.items())'
1000 loops, best of 3: 260 usec per loop
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z2=dict(x, **y)'
10000 loops, best of 3: 26.9 usec per loop
z2
赢了大约10倍。这是我书中相当大的胜利!
比较z1
完这两个项目后,我想知道的不佳表现是否可以归因于构建两个项目列表的开销,这反过来又让我想知道这种变化是否会更好:
from itertools import chain
z3 = dict(chain(x.iteritems(), y.iteritems()))
一些快速测试,例如
% python -m timeit -s 'from itertools import chain; from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z3=dict(chain(x.iteritems(), y.iteritems()))'
10000 loops, best of 3: 66 usec per loop
我得出的结论是,z3
它的速度要比速度快z1
,但不及速度z2
。绝对不值得所有额外的打字。
讨论中仍然缺少一些重要的内容,这是将这些替代方法与合并两个列表的“明显”方法的性能比较:使用该update
方法。为了使事情与表达式保持一致,而不会修改x或y,我将制作x的副本,而不是就地对其进行修改,如下所示:
z0 = dict(x)
z0.update(y)
典型结果:
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z0=dict(x); z0.update(y)'
10000 loops, best of 3: 26.9 usec per loop
换句话说,z0
并且z2
似乎具有基本相同的性能。您认为这可能是巧合吗?我不….
实际上,我什至声称纯粹的Python代码不可能做到比这更好。而且,如果您可以在C扩展模块中做得更好,我想Python人士可能会对将您的代码(或方法的变体)并入Python核心感兴趣。Python dict
在很多地方都使用过;优化运营非常重要。
您也可以这样写
z0 = x.copy()
z0.update(y)
就像Tony一样,但是(并不奇怪)表示法上的差异对性能没有任何可测量的影响。使用对您而言合适的任何一种。当然,他绝对正确地指出,两语句版本更容易理解。
In a follow-up answer, you asked about the relative performance of these two alternatives:
z1 = dict(x.items() + y.items())
z2 = dict(x, **y)
On my machine, at least (a fairly ordinary x86_64 running Python 2.5.2), alternative z2
is not only shorter and simpler but also significantly faster. You can verify this for yourself using the timeit
module that comes with Python.
Example 1: identical dictionaries mapping 20 consecutive integers to themselves:
% python -m timeit -s 'x=y=dict((i,i) for i in range(20))' 'z1=dict(x.items() + y.items())'
100000 loops, best of 3: 5.67 usec per loop
% python -m timeit -s 'x=y=dict((i,i) for i in range(20))' 'z2=dict(x, **y)'
100000 loops, best of 3: 1.53 usec per loop
z2
wins by a factor of 3.5 or so. Different dictionaries seem to yield quite different results, but z2
always seems to come out ahead. (If you get inconsistent results for the same test, try passing in -r
with a number larger than the default 3.)
Example 2: non-overlapping dictionaries mapping 252 short strings to integers and vice versa:
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z1=dict(x.items() + y.items())'
1000 loops, best of 3: 260 usec per loop
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z2=dict(x, **y)'
10000 loops, best of 3: 26.9 usec per loop
z2
wins by about a factor of 10. That’s a pretty big win in my book!
After comparing those two, I wondered if z1
‘s poor performance could be attributed to the overhead of constructing the two item lists, which in turn led me to wonder if this variation might work better:
from itertools import chain
z3 = dict(chain(x.iteritems(), y.iteritems()))
A few quick tests, e.g.
% python -m timeit -s 'from itertools import chain; from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z3=dict(chain(x.iteritems(), y.iteritems()))'
10000 loops, best of 3: 66 usec per loop
lead me to conclude that z3
is somewhat faster than z1
, but not nearly as fast as z2
. Definitely not worth all the extra typing.
This discussion is still missing something important, which is a performance comparison of these alternatives with the “obvious” way of merging two lists: using the update
method. To try to keep things on an equal footing with the expressions, none of which modify x or y, I’m going to make a copy of x instead of modifying it in-place, as follows:
z0 = dict(x)
z0.update(y)
A typical result:
% python -m timeit -s 'from htmlentitydefs import codepoint2name as x, name2codepoint as y' 'z0=dict(x); z0.update(y)'
10000 loops, best of 3: 26.9 usec per loop
In other words, z0
and z2
seem to have essentially identical performance. Do you think this might be a coincidence? I don’t….
In fact, I’d go so far as to claim that it’s impossible for pure Python code to do any better than this. And if you can do significantly better in a C extension module, I imagine the Python folks might well be interested in incorporating your code (or a variation on your approach) into the Python core. Python uses dict
in lots of places; optimizing its operations is a big deal.
You could also write this as
z0 = x.copy()
z0.update(y)
as Tony does, but (not surprisingly) the difference in notation turns out not to have any measurable effect on performance. Use whichever looks right to you. Of course, he’s absolutely correct to point out that the two-statement version is much easier to understand.
回答 6
在Python 3.0及更高版本中,您可以使用collections.ChainMap
将多个字典或其他映射组合在一起的方式来创建一个可更新的视图:
>>> from collections import ChainMap
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = dict(ChainMap({}, y, x))
>>> for k, v in z.items():
print(k, '-->', v)
a --> 1
b --> 10
c --> 11
适用于Python 3.5和更高版本的更新:可以使用PEP 448扩展词典打包和拆包。快速简便:
>>> x = {'a':1, 'b': 2}
>>> y = y = {'b':10, 'c': 11}
>>> {**x, **y}
{'a': 1, 'b': 10, 'c': 11}
In Python 3.0 and later, you can use collections.ChainMap
which groups multiple dicts or other mappings together to create a single, updateable view:
>>> from collections import ChainMap
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = dict(ChainMap({}, y, x))
>>> for k, v in z.items():
print(k, '-->', v)
a --> 1
b --> 10
c --> 11
Update for Python 3.5 and later: You can use PEP 448 extended dictionary packing and unpacking. This is fast and easy:
>>> x = {'a':1, 'b': 2}
>>> y = y = {'b':10, 'c': 11}
>>> {**x, **y}
{'a': 1, 'b': 10, 'c': 11}
回答 7
我想要类似的东西,但是能够指定如何合并重复键上的值,所以我破解了这个(但并未对其进行大量测试)。显然,这不是单个表达式,而是单个函数调用。
def merge(d1, d2, merge_fn=lambda x,y:y):
"""
Merges two dictionaries, non-destructively, combining
values on duplicate keys as defined by the optional merge
function. The default behavior replaces the values in d1
with corresponding values in d2. (There is no other generally
applicable merge strategy, but often you'll have homogeneous
types in your dicts, so specifying a merge technique can be
valuable.)
Examples:
>>> d1
{'a': 1, 'c': 3, 'b': 2}
>>> merge(d1, d1)
{'a': 1, 'c': 3, 'b': 2}
>>> merge(d1, d1, lambda x,y: x+y)
{'a': 2, 'c': 6, 'b': 4}
"""
result = dict(d1)
for k,v in d2.iteritems():
if k in result:
result[k] = merge_fn(result[k], v)
else:
result[k] = v
return result
I wanted something similar, but with the ability to specify how the values on duplicate keys were merged, so I hacked this out (but did not heavily test it). Obviously this is not a single expression, but it is a single function call.
def merge(d1, d2, merge_fn=lambda x,y:y):
"""
Merges two dictionaries, non-destructively, combining
values on duplicate keys as defined by the optional merge
function. The default behavior replaces the values in d1
with corresponding values in d2. (There is no other generally
applicable merge strategy, but often you'll have homogeneous
types in your dicts, so specifying a merge technique can be
valuable.)
Examples:
>>> d1
{'a': 1, 'c': 3, 'b': 2}
>>> merge(d1, d1)
{'a': 1, 'c': 3, 'b': 2}
>>> merge(d1, d1, lambda x,y: x+y)
{'a': 2, 'c': 6, 'b': 4}
"""
result = dict(d1)
for k,v in d2.iteritems():
if k in result:
result[k] = merge_fn(result[k], v)
else:
result[k] = v
return result
回答 8
递归/深度更新字典
def deepupdate(original, update):
"""
Recursively update a dict.
Subdict's won't be overwritten but also updated.
"""
for key, value in original.iteritems():
if key not in update:
update[key] = value
elif isinstance(value, dict):
deepupdate(value, update[key])
return update
示范:
pluto_original = {
'name': 'Pluto',
'details': {
'tail': True,
'color': 'orange'
}
}
pluto_update = {
'name': 'Pluutoo',
'details': {
'color': 'blue'
}
}
print deepupdate(pluto_original, pluto_update)
输出:
{
'name': 'Pluutoo',
'details': {
'color': 'blue',
'tail': True
}
}
感谢rednaw的编辑。
Recursively/deep update a dict
def deepupdate(original, update):
"""
Recursively update a dict.
Subdict's won't be overwritten but also updated.
"""
for key, value in original.iteritems():
if key not in update:
update[key] = value
elif isinstance(value, dict):
deepupdate(value, update[key])
return update
Demonstration:
pluto_original = {
'name': 'Pluto',
'details': {
'tail': True,
'color': 'orange'
}
}
pluto_update = {
'name': 'Pluutoo',
'details': {
'color': 'blue'
}
}
print deepupdate(pluto_original, pluto_update)
Outputs:
{
'name': 'Pluutoo',
'details': {
'color': 'blue',
'tail': True
}
}
Thanks rednaw for edits.
回答 9
我不使用副本时可能想到的最佳版本是:
from itertools import chain
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
dict(chain(x.iteritems(), y.iteritems()))
至少在CPython上,它比快,dict(x.items() + y.items())
但没有快n = copy(a); n.update(b)
。如果更改iteritems()
为items()
,则此版本在Python 3中也可以使用,这是2to3工具自动完成的。
我个人最喜欢这个版本,因为它用一种功能语法很好地描述了我想要的内容。唯一的小问题是,来自y的值优先于来自x的值并不能完全清楚,但是我不认为很难弄清楚。
The best version I could think while not using copy would be:
from itertools import chain
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
dict(chain(x.iteritems(), y.iteritems()))
It’s faster than dict(x.items() + y.items())
but not as fast as n = copy(a); n.update(b)
, at least on CPython. This version also works in Python 3 if you change iteritems()
to items()
, which is automatically done by the 2to3 tool.
Personally I like this version best because it describes fairly good what I want in a single functional syntax. The only minor problem is that it doesn’t make completely obvious that values from y takes precedence over values from x, but I don’t believe it’s difficult to figure that out.
回答 10
Python 3.5(PEP 448)允许使用更好的语法选项:
x = {'a': 1, 'b': 1}
y = {'a': 2, 'c': 2}
final = {**x, **y}
final
# {'a': 2, 'b': 1, 'c': 2}
甚至
final = {'a': 1, 'b': 1, **x, **y}
在Python 3.9中,您还可以使用| 和| =以及下面来自PEP 584的示例
d = {'spam': 1, 'eggs': 2, 'cheese': 3}
e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
d | e
# {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
Python 3.5 (PEP 448) allows a nicer syntax option:
x = {'a': 1, 'b': 1}
y = {'a': 2, 'c': 2}
final = {**x, **y}
final
# {'a': 2, 'b': 1, 'c': 2}
Or even
final = {'a': 1, 'b': 1, **x, **y}
In Python 3.9 you also use | and |= with the below example from PEP 584
d = {'spam': 1, 'eggs': 2, 'cheese': 3}
e = {'cheese': 'cheddar', 'aardvark': 'Ethel'}
d | e
# {'spam': 1, 'eggs': 2, 'cheese': 'cheddar', 'aardvark': 'Ethel'}
回答 11
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = dict(x.items() + y.items())
print z
对于两个字典中都有键的项目,您可以通过将最后一个放在输出中来控制哪一个在输出中结束。
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z = dict(x.items() + y.items())
print z
For items with keys in both dictionaries (‘b’), you can control which one ends up in the output by putting that one last.
回答 12
虽然已经多次回答了该问题,但尚未列出此问题的简单解决方案。
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z4 = {}
z4.update(x)
z4.update(y)
它与上面提到的z0和邪恶z2一样快,但易于理解和更改。
While the question has already been answered several times,
this simple solution to the problem has not been listed yet.
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
z4 = {}
z4.update(x)
z4.update(y)
It is as fast as z0 and the evil z2 mentioned above, but easy to understand and change.
回答 13
def dict_merge(a, b):
c = a.copy()
c.update(b)
return c
new = dict_merge(old, extras)
在这些阴暗而可疑的答案中,这个出色的例子是在Python中合并字典的唯一且唯一的好方法,这是独裁者终身支持的Guido van Rossum本人!有人提出了一半的建议,但没有将其放在函数中。
print dict_merge(
{'color':'red', 'model':'Mini'},
{'model':'Ferrari', 'owner':'Carl'})
给出:
{'color': 'red', 'owner': 'Carl', 'model': 'Ferrari'}
def dict_merge(a, b):
c = a.copy()
c.update(b)
return c
new = dict_merge(old, extras)
Among such shady and dubious answers, this shining example is the one and only good way to merge dicts in Python, endorsed by dictator for life Guido van Rossum himself! Someone else suggested half of this, but did not put it in a function.
print dict_merge(
{'color':'red', 'model':'Mini'},
{'model':'Ferrari', 'owner':'Carl'})
gives:
{'color': 'red', 'owner': 'Carl', 'model': 'Ferrari'}
回答 14
如果您认为lambda是邪恶的,那么请继续阅读。根据要求,您可以使用一个表达式编写快速而高效的内存解决方案:
x = {'a':1, 'b':2}
y = {'b':10, 'c':11}
z = (lambda a, b: (lambda a_copy: a_copy.update(b) or a_copy)(a.copy()))(x, y)
print z
{'a': 1, 'c': 11, 'b': 10}
print x
{'a': 1, 'b': 2}
如上所述,使用两行或编写函数可能是更好的方法。
If you think lambdas are evil then read no further.
As requested, you can write the fast and memory-efficient solution with one expression:
x = {'a':1, 'b':2}
y = {'b':10, 'c':11}
z = (lambda a, b: (lambda a_copy: a_copy.update(b) or a_copy)(a.copy()))(x, y)
print z
{'a': 1, 'c': 11, 'b': 10}
print x
{'a': 1, 'b': 2}
As suggested above, using two lines or writing a function is probably a better way to go.
回答 15
是pythonic。使用理解:
z={i:d[i] for d in [x,y] for i in d}
>>> print z
{'a': 1, 'c': 11, 'b': 10}
Be pythonic. Use a comprehension:
z={i:d[i] for d in [x,y] for i in d}
>>> print z
{'a': 1, 'c': 11, 'b': 10}
回答 16
在python3中,该items
方法不再返回list,而是返回一个view,其作用类似于set。在这种情况下,您将需要使用set联合,因为与的连接+
将不起作用:
dict(x.items() | y.items())
对于2.7版中的类似python3的行为,该viewitems
方法应代替items
:
dict(x.viewitems() | y.viewitems())
无论如何,我还是更喜欢这种表示法,因为将其视为固定的联合运算而不是串联(如标题所示)似乎更为自然。
编辑:
对于python 3还有几点。首先,请注意,dict(x, **y)
除非输入的键y
是字符串,否则该技巧在python 3中将不起作用。
而且,Raymond Hettinger的Chainmap 答案非常优雅,因为它可以将任意数量的dicts作为参数,但是从文档中看,它似乎依次遍历了每次查找的所有dicts列表:
查找顺序搜索基础映射,直到找到密钥为止。
如果您的应用程序中有很多查找,这可能会减慢您的速度:
In [1]: from collections import ChainMap
In [2]: from string import ascii_uppercase as up, ascii_lowercase as lo; x = dict(zip(lo, up)); y = dict(zip(up, lo))
In [3]: chainmap_dict = ChainMap(y, x)
In [4]: union_dict = dict(x.items() | y.items())
In [5]: timeit for k in union_dict: union_dict[k]
100000 loops, best of 3: 2.15 µs per loop
In [6]: timeit for k in chainmap_dict: chainmap_dict[k]
10000 loops, best of 3: 27.1 µs per loop
因此,查找速度要慢一个数量级。我是Chainmap的粉丝,但在可能有很多查找的地方看起来不太实用。
In python3, the items
method no longer returns a list, but rather a view, which acts like a set. In this case you’ll need to take the set union since concatenating with +
won’t work:
dict(x.items() | y.items())
For python3-like behavior in version 2.7, the viewitems
method should work in place of items
:
dict(x.viewitems() | y.viewitems())
I prefer this notation anyways since it seems more natural to think of it as a set union operation rather than concatenation (as the title shows).
Edit:
A couple more points for python 3. First, note that the dict(x, **y)
trick won’t work in python 3 unless the keys in y
are strings.
Also, Raymond Hettinger’s Chainmap answer is pretty elegant, since it can take an arbitrary number of dicts as arguments, but from the docs it looks like it sequentially looks through a list of all the dicts for each lookup:
Lookups search the underlying mappings successively until a key is found.
This can slow you down if you have a lot of lookups in your application:
In [1]: from collections import ChainMap
In [2]: from string import ascii_uppercase as up, ascii_lowercase as lo; x = dict(zip(lo, up)); y = dict(zip(up, lo))
In [3]: chainmap_dict = ChainMap(y, x)
In [4]: union_dict = dict(x.items() | y.items())
In [5]: timeit for k in union_dict: union_dict[k]
100000 loops, best of 3: 2.15 µs per loop
In [6]: timeit for k in chainmap_dict: chainmap_dict[k]
10000 loops, best of 3: 27.1 µs per loop
So about an order of magnitude slower for lookups. I’m a fan of Chainmap, but looks less practical where there may be many lookups.
回答 17
滥用导致马修回答的单一表达解决方案:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = (lambda f=x.copy(): (f.update(y), f)[1])()
>>> z
{'a': 1, 'c': 11, 'b': 10}
您说您想要一个表达式,所以我滥用lambda
了绑定一个名称,并使用元组重写了lambda的一个表达式的限制。随时畏缩。
如果您不关心复制它,当然也可以这样做:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = (x.update(y), x)[1]
>>> z
{'a': 1, 'b': 10, 'c': 11}
Abuse leading to a one-expression solution for Matthew’s answer:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = (lambda f=x.copy(): (f.update(y), f)[1])()
>>> z
{'a': 1, 'c': 11, 'b': 10}
You said you wanted one expression, so I abused lambda
to bind a name, and tuples to override lambda’s one-expression limit. Feel free to cringe.
You could also do this of course if you don’t care about copying it:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> z = (x.update(y), x)[1]
>>> z
{'a': 1, 'b': 10, 'c': 11}
回答 18
使用itertools的简单解决方案,该命令可保留顺序(后格优先)
import itertools as it
merge = lambda *args: dict(it.chain.from_iterable(it.imap(dict.iteritems, args)))
它的用法是:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> merge(x, y)
{'a': 1, 'b': 10, 'c': 11}
>>> z = {'c': 3, 'd': 4}
>>> merge(x, y, z)
{'a': 1, 'b': 10, 'c': 3, 'd': 4}
Simple solution using itertools that preserves order (latter dicts have precedence)
import itertools as it
merge = lambda *args: dict(it.chain.from_iterable(it.imap(dict.iteritems, args)))
And it’s usage:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> merge(x, y)
{'a': 1, 'b': 10, 'c': 11}
>>> z = {'c': 3, 'd': 4}
>>> merge(x, y, z)
{'a': 1, 'b': 10, 'c': 3, 'd': 4}
回答 19
回答 20
即使答案对于此浅表字典而言是好的,但此处定义的方法均未进行深字典合并。
示例如下:
a = { 'one': { 'depth_2': True }, 'two': True }
b = { 'one': { 'extra': False } }
print dict(a.items() + b.items())
人们会期望这样的结果:
{ 'one': { 'extra': False', 'depth_2': True }, 'two': True }
相反,我们得到以下信息:
{'two': True, 'one': {'extra': False}}
如果“ one”条目确实是合并的,则其字典中的项目应具有“ depth_2”和“ extra”作为条目。
也使用链,不起作用:
from itertools import chain
print dict(chain(a.iteritems(), b.iteritems()))
结果是:
{'two': True, 'one': {'extra': False}}
rcwesick进行的深度合并也产生相同的结果。
是的,可以合并示例字典,但是它们都不是合并的通用机制。一旦编写了可以真正合并的方法,我将在以后进行更新。
Even though the answers were good for this shallow dictionary, none of the methods defined here actually do a deep dictionary merge.
Examples follow:
a = { 'one': { 'depth_2': True }, 'two': True }
b = { 'one': { 'extra': False } }
print dict(a.items() + b.items())
One would expect a result of something like this:
{ 'one': { 'extra': False', 'depth_2': True }, 'two': True }
Instead, we get this:
{'two': True, 'one': {'extra': False}}
The ‘one’ entry should have had ‘depth_2’ and ‘extra’ as items inside its dictionary if it truly was a merge.
Using chain also, does not work:
from itertools import chain
print dict(chain(a.iteritems(), b.iteritems()))
Results in:
{'two': True, 'one': {'extra': False}}
The deep merge that rcwesick gave also creates the same result.
Yes, it will work to merge the sample dictionaries, but none of them are a generic mechanism to merge. I’ll update this later once I write a method that does a true merge.
回答 21
(仅适用于Python2.7 *;对于Python3 *有更简单的解决方案。)
如果您不反对导入标准库模块,则可以执行
from functools import reduce
def merge_dicts(*dicts):
return reduce(lambda a, d: a.update(d) or a, dicts, {})
(由于总是返回成功,所以中的or a
位lambda
是必需的。)dict.update
None
(For Python2.7* only; there are simpler solutions for Python3*.)
If you’re not averse to importing a standard library module, you can do
from functools import reduce
def merge_dicts(*dicts):
return reduce(lambda a, d: a.update(d) or a, dicts, {})
(The or a
bit in the lambda
is necessary because dict.update
always returns None
on success.)
回答 22
如果您不介意变异x
,
x.update(y) or x
简单,可读,高效。您知道 update()
总是会返回None
,这是一个错误的值。因此,上述表达式x
在更新后将始终等于。
标准库中的变异方法(如.update()
)None
按约定返回,因此该模式也适用于这些方法。如果您使用的方法不遵循此约定,则or
可能无法正常工作。但是,您可以使用元组显示和索引来使其成为单个表达式。无论第一个元素的计算结果如何,此方法都有效。
(x.update(y), x)[-1]
如果还没有x
变量,则可以使用lambda
本地变量而不使用赋值语句。这相当于lambda
用作let表达式,这是功能语言中的一种常用技术,但可能不是Python语言。
(lambda x: x.update(y) or x)({'a': 1, 'b': 2})
尽管与下面使用新的walrus运算符(仅适用于Python 3.8+)没有什么不同:
(x := {'a': 1, 'b': 2}).update(y) or x
如果您确实想要复制,则PEP 448样式最简单{**x, **y}
。但是,如果您的(旧)Python版本中没有该功能,则let模式也可以在这里使用。
(lambda z: z.update(y) or z)(x.copy())
(当然,这等效于(z := x.copy()).update(y) or z
,但是如果您的Python版本足够新,则可以使用PEP 448样式。)
If you don’t mind mutating x
,
x.update(y) or x
Simple, readable, performant. You know update()
always returns None
, which is a false value. So the above expression will always evaluate to x
, after updating it.
Mutating methods in the standard library (like .update()
) return None
by convention, so this pattern will work on those too. If you’re using a method that doesn’t follow this convention, then or
may not work. But, you can use a tuple display and index to make it a single expression, instead. This works regardless of what the first element evaluates to.
(x.update(y), x)[-1]
If you don’t have x
in a variable yet, you can use lambda
to make a local without using an assignment statement. This amounts to using lambda
as a let expression, which is a common technique in functional languages, but maybe unpythonic.
(lambda x: x.update(y) or x)({'a': 1, 'b': 2})
Although it’s not that different from the following use of the new walrus operator (Python 3.8+ only):
(x := {'a': 1, 'b': 2}).update(y) or x
If you do want a copy, PEP 448 style is easiest {**x, **y}
. But if that’s not available in your (older) Python version, the let pattern works here too.
(lambda z: z.update(y) or z)(x.copy())
(That is, of course, equivalent to (z := x.copy()).update(y) or z
, but if your Python version is new enough for that, then the PEP 448 style will be available.)
回答 23
借鉴这里和其他地方的想法,我理解了一个函数:
def merge(*dicts, **kv):
return { k:v for d in list(dicts) + [kv] for k,v in d.items() }
用法(在python 3中测试):
assert (merge({1:11,'a':'aaa'},{1:99, 'b':'bbb'},foo='bar')==\
{1: 99, 'foo': 'bar', 'b': 'bbb', 'a': 'aaa'})
assert (merge(foo='bar')=={'foo': 'bar'})
assert (merge({1:11},{1:99},foo='bar',baz='quux')==\
{1: 99, 'foo': 'bar', 'baz':'quux'})
assert (merge({1:11},{1:99})=={1: 99})
您可以改用lambda。
Drawing on ideas here and elsewhere I’ve comprehended a function:
def merge(*dicts, **kv):
return { k:v for d in list(dicts) + [kv] for k,v in d.items() }
Usage (tested in python 3):
assert (merge({1:11,'a':'aaa'},{1:99, 'b':'bbb'},foo='bar')==\
{1: 99, 'foo': 'bar', 'b': 'bbb', 'a': 'aaa'})
assert (merge(foo='bar')=={'foo': 'bar'})
assert (merge({1:11},{1:99},foo='bar',baz='quux')==\
{1: 99, 'foo': 'bar', 'baz':'quux'})
assert (merge({1:11},{1:99})=={1: 99})
You could use a lambda instead.
回答 24
迄今为止,我列出的解决方案存在的问题是,在合并的词典中,键“ b”的值是10,但按照我的想法,应该是12。鉴于此,我提出以下内容:
import timeit
n=100000
su = """
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
"""
def timeMerge(f,su,niter):
print "{:4f} sec for: {:30s}".format(timeit.Timer(f,setup=su).timeit(n),f)
timeMerge("dict(x, **y)",su,n)
timeMerge("x.update(y)",su,n)
timeMerge("dict(x.items() + y.items())",su,n)
timeMerge("for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k] ",su,n)
#confirm for loop adds b entries together
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k]
print "confirm b elements are added:",x
结果:
0.049465 sec for: dict(x, **y)
0.033729 sec for: x.update(y)
0.150380 sec for: dict(x.items() + y.items())
0.083120 sec for: for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k]
confirm b elements are added: {'a': 1, 'c': 11, 'b': 12}
The problem I have with solutions listed to date is that, in the merged dictionary, the value for key “b” is 10 but, to my way of thinking, it should be 12.
In that light, I present the following:
import timeit
n=100000
su = """
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
"""
def timeMerge(f,su,niter):
print "{:4f} sec for: {:30s}".format(timeit.Timer(f,setup=su).timeit(n),f)
timeMerge("dict(x, **y)",su,n)
timeMerge("x.update(y)",su,n)
timeMerge("dict(x.items() + y.items())",su,n)
timeMerge("for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k] ",su,n)
#confirm for loop adds b entries together
x = {'a':1, 'b': 2}
y = {'b':10, 'c': 11}
for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k]
print "confirm b elements are added:",x
Results:
0.049465 sec for: dict(x, **y)
0.033729 sec for: x.update(y)
0.150380 sec for: dict(x.items() + y.items())
0.083120 sec for: for k in y.keys(): x[k] = k in x and x[k]+y[k] or y[k]
confirm b elements are added: {'a': 1, 'c': 11, 'b': 12}
回答 25
真傻,.update
什么也没返回。
我只是使用一个简单的辅助函数来解决问题:
def merge(dict1,*dicts):
for dict2 in dicts:
dict1.update(dict2)
return dict1
例子:
merge(dict1,dict2)
merge(dict1,dict2,dict3)
merge(dict1,dict2,dict3,dict4)
merge({},dict1,dict2) # this one returns a new copy
It’s so silly that .update
returns nothing.
I just use a simple helper function to solve the problem:
def merge(dict1,*dicts):
for dict2 in dicts:
dict1.update(dict2)
return dict1
Examples:
merge(dict1,dict2)
merge(dict1,dict2,dict3)
merge(dict1,dict2,dict3,dict4)
merge({},dict1,dict2) # this one returns a new copy
回答 26
from collections import Counter
dict1 = {'a':1, 'b': 2}
dict2 = {'b':10, 'c': 11}
result = dict(Counter(dict1) + Counter(dict2))
这应该可以解决您的问题。
from collections import Counter
dict1 = {'a':1, 'b': 2}
dict2 = {'b':10, 'c': 11}
result = dict(Counter(dict1) + Counter(dict2))
This should solve your problem.
回答 27
这可以通过单个dict理解来完成:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> { key: y[key] if key in y else x[key]
for key in set(x) + set(y)
}
在我看来,“单个表达式”部分的最佳答案是因为不需要额外的功能,而且它很简短。
This can be done with a single dict comprehension:
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> { key: y[key] if key in y else x[key]
for key in set(x) + set(y)
}
In my view the best answer for the ‘single expression’ part as no extra functions are needed, and it is short.
回答 28
由于PEP 572:Assignment Expressions,Python 3.8发行版(计划于2019年10月20日)将提供一个新选项。新的赋值表达式运算符使您可以分配的结果,并仍然使用它来调用,从而使组合的代码成为单个表达式,而不是两个语句,从而进行了更改::=
copy
update
newdict = dict1.copy()
newdict.update(dict2)
至:
(newdict := dict1.copy()).update(dict2)
同时在各个方面都表现相同。如果还必须返回结果dict
(您要求返回的表达式dict
;上面创建并分配给newdict
,但没有返回,因此您不能使用它将参数直接传递给函数la myfunc((newdict := dict1.copy()).update(dict2))
) ,然后将其添加or newdict
到末尾(因为update
returns None
是虚假的,因此它将求值并newdict
作为表达式的结果返回):
(newdict := dict1.copy()).update(dict2) or newdict
重要警告:通常,我不建议采用以下方法:
newdict = {**dict1, **dict2}
拆包方法更清晰(对于一开始就知道要进行广义拆包的人来说,应该这样),根本不需要名称(因此,构造一个立即传递给a的临时文件时,它会更加简洁。函数或包含在list
/ tuple
文字等中),并且几乎肯定也更快,因为(在CPython上)大致等同于:
newdict = {}
newdict.update(dict1)
newdict.update(dict2)
但使用具体的dict
API 在C层完成,因此不涉及动态方法查找/绑定或函数调用分派开销(在此(newdict := dict1.copy()).update(dict2)
情况下,行为不可避免地与原始的两层相同,在不连续的步骤中执行工作,并进行动态查找/绑定/方法的调用。
它也更可扩展,因为合并三个dict
s是显而易见的:
newdict = {**dict1, **dict2, **dict3}
使用赋值表达式不会像这样缩放的地方;您能得到的最接近的是:
(newdict := dict1.copy()).update(dict2), newdict.update(dict3)
或没有None
s 的临时元组,但对每个None
结果进行真实性测试:
(newdict := dict1.copy()).update(dict2) or newdict.update(dict3)
其中的任一个是明显更恶心,并且包括进一步的低效(或者是临时浪费tuple
的None
S表示逗号分离,或每个的无意义感实性测试update
的None
用于返回or
分离)。
赋值表达式方法的唯一真正优势在于:
- 您有需要同时处理
set
s和dict
s的通用代码(它们都支持copy
和update
,因此代码可以按您期望的那样大致工作)
- 您期望接收任意类似dict的对象,而不仅仅是
dict
自身,并且必须保留左侧的类型和语义(而不是以简单的结尾dict
)。尽管myspecialdict({**speciala, **specialb})
可能会起作用,但它会涉及一个额外的临时操作dict
,并且如果myspecialdict
具有平原dict
无法保留的功能(例如,常规dict
s现在基于键的首次出现保留顺序,而基于键的最后出现保留值;您可能想要一个根据最后一个保留订单键的外观,因此更新值也会将其移到末尾),那么语义将是错误的。由于赋值表达式版本使用命名方法(可能会重载以使其正常运行),因此它根本不会创建一个dict
(除非dict1
已经是一个dict
),并保留原始类型(和原始类型的语义),同时避免任何临时性。
There will be a new option when Python 3.8 releases (scheduled for 20 October, 2019), thanks to PEP 572: Assignment Expressions. The new assignment expression operator :=
allows you to assign the result of the copy
and still use it to call update
, leaving the combined code a single expression, rather than two statements, changing:
newdict = dict1.copy()
newdict.update(dict2)
to:
(newdict := dict1.copy()).update(dict2)
while behaving identically in every way. If you must also return the resulting dict
(you asked for an expression returning the dict
; the above creates and assigns to newdict
, but doesn’t return it, so you couldn’t use it to pass an argument to a function as is, a la myfunc((newdict := dict1.copy()).update(dict2))
), then just add or newdict
to the end (since update
returns None
, which is falsy, it will then evaluate and return newdict
as the result of the expression):
(newdict := dict1.copy()).update(dict2) or newdict
Important caveat: In general, I’d discourage this approach in favor of:
newdict = {**dict1, **dict2}
The unpacking approach is clearer (to anyone who knows about generalized unpacking in the first place, which you should), doesn’t require a name for the result at all (so it’s much more concise when constructing a temporary that is immediately passed to a function or included in a list
/tuple
literal or the like), and is almost certainly faster as well, being (on CPython) roughly equivalent to:
newdict = {}
newdict.update(dict1)
newdict.update(dict2)
but done at the C layer, using the concrete dict
API, so no dynamic method lookup/binding or function call dispatch overhead is involved (where (newdict := dict1.copy()).update(dict2)
is unavoidably identical to the original two-liner in behavior, performing the work in discrete steps, with dynamic lookup/binding/invocation of methods.
It’s also more extensible, as merging three dict
s is obvious:
newdict = {**dict1, **dict2, **dict3}
where using assignment expressions won’t scale like that; the closest you could get would be:
(newdict := dict1.copy()).update(dict2), newdict.update(dict3)
or without the temporary tuple of None
s, but with truthiness testing of each None
result:
(newdict := dict1.copy()).update(dict2) or newdict.update(dict3)
either of which is obviously much uglier, and includes further inefficiencies (either a wasted temporary tuple
of None
s for comma separation, or pointless truthiness testing of each update
‘s None
return for or
separation).
The only real advantage to the assignment expression approach occurs if:
- You have generic code that needs handle both
set
s and dict
s (both of them support copy
and update
, so the code works roughly as you’d expect it to)
- You expect to receive arbitrary dict-like objects, not just
dict
itself, and must preserve the type and semantics of the left hand side (rather than ending up with a plain dict
). While myspecialdict({**speciala, **specialb})
might work, it would involve an extra temporary dict
, and if myspecialdict
has features plain dict
can’t preserve (e.g. regular dict
s now preserve order based on the first appearance of a key, and value based on the last appearance of a key; you might want one that preserves order based on the last appearance of a key so updating a value also moves it to the end), then the semantics would be wrong. Since the assignment expression version uses the named methods (which are presumably overloaded to behave appropriately), it never creates a dict
at all (unless dict1
was already a dict
), preserving the original type (and original type’s semantics), all while avoiding any temporaries.
回答 29
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> x, z = dict(x), x.update(y) or x
>>> x
{'a': 1, 'b': 2}
>>> y
{'c': 11, 'b': 10}
>>> z
{'a': 1, 'c': 11, 'b': 10}
>>> x = {'a':1, 'b': 2}
>>> y = {'b':10, 'c': 11}
>>> x, z = dict(x), x.update(y) or x
>>> x
{'a': 1, 'b': 2}
>>> y
{'c': 11, 'b': 10}
>>> z
{'a': 1, 'c': 11, 'b': 10}