Python“扩展"对于字典 [英] Python "extend" for a dictionary

查看:18
本文介绍了Python“扩展"对于字典的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在避免使用 for 循环的同时,用另一个字典扩展字典的最佳方法是什么?例如:

<预><代码>>>>a = {a": 1, b": 2 }>>>b = {c": 3, d": 4 }>>>一个{'a':1,'b':2}>>>乙{'c':3,'d':4}

结果:

{ "a";: 1, b": 2, c": 3, d": 4 }

类似于:

a.extend(b) # 这不起作用

解决方案

a.update(b)

最新的 Python 标准库文档

What is the best way to extend a dictionary with another one while avoiding the use of a for loop? For instance:

>>> a = { "a" : 1, "b" : 2 }
>>> b = { "c" : 3, "d" : 4 }
>>> a
{'a': 1, 'b': 2}
>>> b
{'c': 3, 'd': 4}

Result:

{ "a" : 1, "b" : 2, "c" : 3, "d" : 4 }

Something like:

a.extend(b)  # This does not work

解决方案

a.update(b)

Latest Python Standard Library Documentation

这篇关于Python“扩展"对于字典的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆