将字典加在一起,Python [英] Adding dictionaries together, Python
本文介绍了将字典加在一起,Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我有两本词典,我希望能够将它们制作成一本:
I have two dictionaries and I'd like to be able to make them one:
像这样的伪 Python 会很好:
Something like this pseudo-Python would be nice:
dic0 = {'dic0': 0}
dic1 = {'dic1': 1}
ndic = dic0 + dic1
# ndic would equal {'dic0': 0, 'dic1': 1}
推荐答案
如果您有兴趣在不使用中间存储的情况下创建新的 dict :(这比使用 dict 更快,而且在我看来更干净.items()
)
If you're interested in creating a new dict without using intermediary storage: (this is faster, and in my opinion, cleaner than using dict.items()
)
dic2 = dict(dic0, **dic1)
或者,如果您愿意使用现有的字典之一:
Or if you're happy to use one of the existing dicts:
dic0.update(dic1)
这篇关于将字典加在一起,Python的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文