将字典附加到字典? [重复]

本文翻译自:Append dictionary to a dictionary? [duplicate]

This question already has an answer here: 这个问题在这里已有答案:

I have two existing dictionaries, and I wish to 'append' one of them to the other. 我有两个现有的词典,我希望将其中一个词典“追加”到另一个词典中。 By that I mean that the key,values of the other dictionary should be made into the first dictionary. 我的意思是,其他字典的关键值应该被制作成第一个字典。 For example: 例如:

orig = {
   'A': 1,
   'B': 2,
   'C': 3,
}

extra = {
   'D': 4,
   'E': 5,
}

dest = # something here involving orig and extra

print dest
{
   'A': 1,
   'B': 2,
   'C': 3,
   'D': 4,
   'E': 5
}

I think this all can be achieved through a for loop (maybe?), but is there some method of dictionaries or any other module that saves this job for me? 我认为这一切都可以通过for循环实现(也许?),但是有一些字典方法或任何其他模块可以为我保存这份工作吗? The actual dictionaries I'm using are really big... 我正在使用的实际词典真的很大......


#1楼

参考:https://stackoom.com/question/Btl1/将字典附加到字典-重复


#2楼

The answer I want to give is "use collections.ChainMap", but I just discovered that it was only added in Python 3.3: https://docs.python.org/3.3/library/collections.html#chainmap-objects 我想给出的答案是“使用collections.ChainMap”,但我刚刚发现它只是在Python 3.3中添加: https//docs.python.org/3.3/library/collections.html#chainmap-objects

You can try to crib the class from the 3.3 source though: http://hg.python.org/cpython/file/3.3/Lib/collections/ init .py#l763 您可以尝试从3.3源代码中删除类: http//hg.python.org/cpython/file/3.3/Lib/collections/ init .py#l763

Here is a less feature-full Python 2.x compatible version (same author): http://code.activestate.com/recipes/305268-chained-map-lookups/ 这是一个功能较少的Python 2.x兼容版本(同一作者): http//code.activestate.com/recipes/305268-chained-map-lookups/

Instead of expanding/overwriting one dictionary with another using dict.merge, or creating an additional copy merging both, you create a lookup chain that searches both in order. 不是使用dict.merge扩展/覆盖另一个字典,也不是创建合并两者的附加副本,而是创建一个按顺序搜索的查找链。 Because it doesn't duplicate the mappings it wraps ChainMap uses very little memory, and sees later modifications to any sub-mapping. 因为它不会复制它包装的映射,所以ChainMap使用的内存非常少,并且可以看到以后对任何子映射的修改。 Because order matters you can also use the chain to layer defaults (ie user prefs > config > env). 因为顺序很重要,您还可以使用链来分层默认值(即用户首选项> config> env)。


#3楼

The most pythonic (and slightly faster) way to accomplish this is by: 最pythonic(和稍快)的方法是通过:

dest = {**orig, **extra}

Or, depending on the problem to solve, maybe: 或者,根据要解决的问题,可能:

dest = {**orig, 'D': 4, 'E': 5}

#4楼

A three-liner to combine or merge two dictionaries: 组合或合并两个词典的三线程:

dest = {}
dest.update(orig)
dest.update(extra)

This creates a new dictionary dest without modifying orig and extra . 这会创建一个新的字典dest而不修改origextra

Note: If a key has different values in orig and extra , then extra overrides orig . 注意:如果一个键在origextra有不同的值,那么extra覆盖orig


#5楼

dict.update() looks like it will do what you want... dict.update()看起来会做你想要的......

>> orig.update(extra)
>>> orig
{'A': 1, 'C': 3, 'B': 2, 'E': 5, 'D': 4}
>>> 

Perhaps, though, you don't want to update your original dictionary, but work on a copy: 但是,您可能不希望更新原始字典,但需要处理副本:

>>> dest = orig.copy()
>>> dest.update(extra)
>>> orig
{'A': 1, 'C': 3, 'B': 2}
>>> dest
{'A': 1, 'C': 3, 'B': 2, 'E': 5, 'D': 4}

#6楼

There is the .update() method :) 有.update()方法:)

update([other]) Update the dictionary with the key/value pairs from other, overwriting existing keys. 更新([other])使用其他键中的键/值对更新字典,覆盖现有键。 Return None. 返回无。

update() accepts either another dictionary object or an iterable of key/value pairs (as tuples or other iterables of length two). update()接受另一个字典对象或一对键/值对的迭代(作为元组或长度为2的其他迭代)。 If keyword arguments are specified, the dictionary is then updated with those key/value pairs: d.update(red=1, blue=2). 如果指定了关键字参数,则使用这些键/值对更新字典:d.update(red = 1,blue = 2)。

Changed in version 2.4: Allowed the argument to be an iterable of key/value pairs and allowed keyword arguments. 版本2.4中更改:允许参数为键/值对的可迭代,并允许关键字参数。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值