本文介绍了如何在Python中的一个表达式中合并两个词典(合并词典)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧! 问题描述 29岁程序员,3月因学历无情被辞! 我有两个Python字典,我想编写一个返回这两个字典的表达式(合并)(即合并)。 code>,依此类推。 其他答案的批判 请勿使用您在先前接受的答案中看到的内容: z = dict(x.items()+ y.items()) 在Python 2中,您为每个字典在内存中创建两个列表,在内存中创建第三个列表,其长度等于前两个单词的长度,然后丢弃所有三个列表以创建字典。 在Python 3中,此操作将失败,因为您要一起添加两个 dict_items 对象,而不是两个列表- >>> c = dict(a.items()+ b.items())追溯(最近一次调用为最后):文件< stdin>,第1行,在< module>中。 TypeError:+不支持的操作数类型:'dict_items'和'dict_items' 必须将它们显式创建为列表,例如 z = dict(list(x.items())+ list(y.items()))。 类似地,在Python 3中使用 items()的并集( viewitems()也会失败。即使您的值是可散列的,由于集合在语义上是无序的,因此在优先级方面行为是不确定的。因此,请勿这样做: >>> c = dict(a.items()| b.items()) 此示例演示了值不可散列时的情况: >>>: x = {’a’:[]} >>> y = {’b’:[]} >>> dict(x.items()| y.items())追溯(最近一次调用为最后一次):文件< stdin>,第1行,在< module>中。 TypeError:不可散列的类型:'list' 这里是一个示例,其中y应该优先,但是取值由于集合的任意顺序,保留了x中的: >> x = {’a’:2} >>> y = {’a’:1} >>> dict(x.items()| y.items()) {'a':2} 您不应该使用的另一种技巧: z = dict(x,** y) 这使用了 dict 构造函数,并且非常快且内存高效(甚至稍微多一些-因此,比起我们的两步过程),除非您确切地知道发生了什么(也就是说,第二个dict作为关键字参数传递给dict构造函数),否则很难阅读,这不是预期的用法,因此不是Pythonic。 以下是用法示例: 字典旨在获取可散列的键(例如,冻结集或元组),但是此方法在Python 3中失败当键不是字符串时。 >>> c = dict(a,** b)追溯(最近一次调用为最新):文件< stdin>,第1行,在< module>中。 TypeError:关键字参数必须是字符串 来自邮件列表,该语言的创建者Guido van Rossum写道:和这是我的理解(以及对语言的创建者)表示 dict的预期用法(** y)用于出于可读性目的创建字典,例如: dict (a = 1,b = 10,c = 11) 而不是 {'a':1,'b':10,'c':11} 回复评论 当键为非字符串时,它不适用于3。隐式调用协定是名称空间采用普通字典,而用户只能传递字符串形式的关键字参数。所有其他可调用对象都强制执行它。 dict 在Python 2中破坏了这种一致性: >> foo(** {('a','b'):None})追溯(最近一次通话结束):文件< stdin>,第1行,< > TypeError:foo()关键字必须为字符串>>> dict(** {('a','b'):无}) {('a','b'):无} 考虑到其他Python实现(Pypy,Jython,IronPython),这种不一致是很严重的。因此,它在Python 3中已得到修复,因为这种用法可能是一个重大更改。 我向您表示,故意编写仅适用于一种语言版本的代码是一种恶意的无能。 更多评论:我的回答: merge_two_dicts(x,y)实际上对我来说很清晰,如果我们实际上担心可读性。而且它不向前兼容,因为Python 2越来越不推荐使用。是。我必须回头再问这个问题,它要求 两个 字典的 shallow 合并,第一个字典的值被第二个字典的值覆盖-在一个表达式中。 假设两个字典,一个字典可能会递归地将它们合并到一个函数中,但是您应注意不要从任何一个源修改字典,最可靠的是避免这种情况的方法是在分配值时进行复制。由于键必须是可散列的,因此通常是不可变的,因此将它们复制是毫无意义的: from copy import deepcopy def dict_of_dicts_merge(x,y):z = {} overlay_keys = x.keys()& y.keys()表示重叠键中的键:z [key] = dict_of_dicts_merge(x [key],y [key])表示x.keys()中的键-overlay_keys:z [key] = deepcopy(x [key])作为y.keys()中的密钥:z [key] = deepcopy(y [key])返回z 用法: >> x = {’a’:{1:{}},‘b’:{2:{}}} >>> y = {’b’:{10:{}},‘c’:{11:{}}} >>> dict_of_dicts_merge(x,y) {'b':{2:{},10:{}},'a':{1:{}},'c':{11:{}}} 想出其他值类型的偶然性远远超出了此问题的范围,因此我将向您指出我对字典词典合并的规范问题的回答。 表现欠佳但是,正确的临时选择 这些方法的性能较差,但它们将提供正确的行为。 与复制和更新或新版本相比,它们的性能要差得多。之所以进行解压缩,是因为它们在更高的抽象级别上遍历每个键值对,但是它们 do 遵守优先级顺序(后者具有优先级) 您还可以在 dict理解力: {k:v for d表示dicts for k,v表示d.items()}#Python 2.7中的迭代项 或在python 2.6中(也许早在引入生成器表达式时为2.4): dict((k,v)for d表示字典,k,v表示d.items())#Python 2中的迭代项 itertools.chain 将按正确的顺序将迭代器链接到键值对上: 从itertools导入链z = dict(chain(chain(x.items(),y.items()))#Python 2中的迭代项 性能分析 我只打算对已知行为正确的用途进行性能分析。 (自包含,因此您可以复制和粘贴自己。) from timeit import从itertools重复导入链 x = dict.fromkeys('abcdefg')y = dict.fromkeys('efghijk') def merge_two_dicts(x,y):z = x.copy() z.update(y) return z min(repeat(lambda:{** x,** y})) min(repeat(lambda:merge_two_dicts(x,y))) min(repeat(lambda:{k:v for d in(x,y)for k,v in d.items()} )) min(repeat(lambda:dict(chain(x.items(),y.items()))))) min(repeat(lambda:dict(item for d in(x, y)用于d.items()))) 在Python 3.8.1中,NixOS: >>> min(repeat(lambda:{** x,** y})) 1.0804965235292912 > min(repeat(lambda:merge_two_dicts(x,y)))) 1.636518670246005 >> min(repeat(lambda:{k:v for d in(x,y)for k,v in d.items()}))) 3.1779992282390594 >> min(repeat(lambda:dict(chain(x.items(),y.items())))) 2.740647904574871 >> min(repeat(lambda:dict(d为(x,y)中d的项目,d.items()中的项目为项目)))) 4.266070580109954 $ uname -a Linux nixos 4.19.113#1-NixOS SMP 3月25日星期三07:06:15 UTC 2020 x86_64 GNU / Linux 词典资源 我对Python的字典实现的解释,已更新为3.6。 有关如何向字典添加新键的答案 将两个列表映射到字典中 官方Python 字典上的文档 更强大的词典-布兰登·罗德斯在Pycon 2017上的演讲 现代Python词典,一个很棒的主意-Raymond Hettinger在Pycon 2017上的演讲 I have two Python dictionaries, and I want to write a single expression that returns these two dictionaries, merged (i.e. taking the union). 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.) 解决方案 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 zand now:z = merge_two_dicts(x, y)In Python 3.9.0 or greater (released 17 October 2020): PEP-584, discussed here, was implemented to further simplify this:z = x | y # NOTE: 3.9+ ONLYExplanationSay you have two dictionaries and you want to merge them into a new dict without altering the original dictionaries: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 dictionary'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, isz = {**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 zIn 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 expressionIf 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 dictionaries, merge them into a new dict as a shallow copy.""" z = x.copy() z.update(y) return zand then you have a single expression:z = merge_two_dicts(x, y)You can also make a function to merge an undefined number of dictionaries, from zero to a very large number:def merge_dicts(*dict_args): """ Given any number of dictionaries, shallow copy and merge into a new dict, precedence goes to key value pairs in latter dictionaries. """ result = {} for dictionary in dict_args: result.update(dictionary) return resultThis function will work in Python 2 and 3 for all dictionaries. e.g. given dictionaries a to g:z = merge_dicts(a, b, c, d, e, f, g)and key value pairs in g will take precedence over dictionaries a to f, and so on.Critiques of Other AnswersDon'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.Dictionaries 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 stringsFrom the mailing list, Guido van Rossum, the creator of the language, wrote:andIt is my understanding (as well as the understanding of the creator of the language) that the intended usage for dict(**y) is for creating dictionaries for readability purposes, e.g.:dict(a=1, b=10, c=11)instead of{'a': 1, 'b': 10, 'c': 11}Response to commentsAgain, it doesn't work for 3 when keys are non-strings. The implicit calling contract is that namespaces take ordinary dictionaries, 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: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.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 dictionaries 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 deepcopydef 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 zUsage:>>> 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-hocsThese 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 dictionaries have precedence)You can also chain the dictionaries manually inside a dict comprehension:{k: v for d in dicts for k, v in d.items()} # iteritems in Python 2.7or 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()) # iteritems in Python 2itertools.chain will chain the iterators over the key-value pairs in the correct order:from itertools import chainz = dict(chain(x.items(), y.items())) # iteritems in Python 2Performance AnalysisI'm only going to do the performance analysis of the usages known to behave correctly. (Self-contained so you can copy and paste yourself.)from timeit import repeatfrom itertools import chainx = dict.fromkeys('abcdefg')y = dict.fromkeys('efghijk')def merge_two_dicts(x, y): z = x.copy() z.update(y) return zmin(repeat(lambda: {**x, **y}))min(repeat(lambda: merge_two_dicts(x, y)))min(repeat(lambda: {k: v for d in (x, y) for k, v in d.items()}))min(repeat(lambda: dict(chain(x.items(), y.items()))))min(repeat(lambda: dict(item for d in (x, y) for item in d.items())))In Python 3.8.1, NixOS:>>> min(repeat(lambda: {**x, **y}))1.0804965235292912>>> min(repeat(lambda: merge_two_dicts(x, y)))1.636518670246005>>> min(repeat(lambda: {k: v for d in (x, y) for k, v in d.items()}))3.1779992282390594>>> min(repeat(lambda: dict(chain(x.items(), y.items()))))2.740647904574871>>> min(repeat(lambda: dict(item for d in (x, y) for item in d.items())))4.266070580109954$ uname -aLinux nixos 4.19.113 #1-NixOS SMP Wed Mar 25 07:06:15 UTC 2020 x86_64 GNU/LinuxResources on DictionariesMy explanation of Python's dictionary implementation, updated for 3.6.Answer on how to add new keys to a dictionaryMapping two lists into a dictionaryThe official Python docs on dictionariesThe Dictionary Even Mightier - talk by Brandon Rhodes at Pycon 2017Modern Python Dictionaries, A Confluence of Great Ideas - talk by Raymond Hettinger at Pycon 2017 这篇关于如何在Python中的一个表达式中合并两个词典(合并词典)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持! 上岸,阿里云!
08-14 11:08