问题描述
在python 3.5中,我们可以使用double-splat解包来合并字典
In python 3.5, we can merge dicts by using double-splat unpacking
>>> d1 = {1: 'one', 2: 'two'}
>>> d2 = {3: 'three'}
>>> {**d1, **d2}
{1: 'one', 2: 'two', 3: 'three'}
很酷.不过,它似乎并没有推广到动态用例:
Cool. It doesn't seem to generalise to dynamic use cases, though:
>>> ds = [d1, d2]
>>> {**d for d in ds}
SyntaxError: dict unpacking cannot be used in dict comprehension
相反,我们必须执行reduce(lambda x,y: {**x, **y}, ds, {})
,这看起来很丑陋.当该表达式似乎没有任何歧义时,为什么解析器不允许一种明显的方法"?
Instead we have to do reduce(lambda x,y: {**x, **y}, ds, {})
, which seems a lot uglier. Why the "one obvious way to do it" is not allowed by the parser, when there doesn't seem to be any ambiguity in that expression?
推荐答案
这并不完全是您的问题的答案,但我会考虑使用 ChainMap
是一种惯用而优雅的方式来完成您的建议(在线合并字典):
It's not exactly an answer to your question but I'd consider using ChainMap
to be an idiomatic and elegant way to do what you propose (merging dictionaries in-line):
>>> from collections import ChainMap
>>> d1 = {1: 'one', 2: 'two'}
>>> d2 = {3: 'three'}
>>> ds = [d1, d2]
>>> dict(ChainMap(*ds))
{1: 'one', 2: 'two', 3: 'three'}
尽管这不是一个特别透明的解决方案,但是由于许多程序员可能不完全了解ChainMap
的工作方式.请注意(如@AnttiHaapala所指出的),首先找到被使用"是这样,因此,根据您的意图,您可能需要先调用reversed
,然后再将dict
传递到ChainMap
.
Although it's not a particularly transparent solution, since many programmers might not know exactly how a ChainMap
works. Note that (as @AnttiHaapala points out) "first found is used" so, depending on your intentions you might need to make a call to reversed
before passing your dict
s into ChainMap
.
>>> d2 = {3: 'three', 2:'LOL'}
>>> dict(ChainMap(*ds))
{1: 'one', 2: 'two', 3: 'three'}
>>> dict(ChainMap(*reversed(ds)))
{1: 'one', 2: 'LOL', 3: 'three'}
这篇关于字典合并中的字典合并的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!