将字典添加在一起,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()更干净) p>
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屋!
查看全文