本篇阅读的代码实现了合并多个字典的功能。 本篇阅读的代码片段来自于30-seconds-of-python。
Python 代码阅读合集介绍:为什么不推荐Python初学者直接看项目源码
本篇阅读的代码实现了合并多个字典的功能。
本篇阅读的代码片段来自于30-seconds-of-python。
merge_dictionaries
def merge_dictionaries(*dicts):
res = dict()
for d in dicts:
res.update(d)
return res
# EXAMPLES
ages_one = {'Peter': 10, 'Isabel': 11}
ages_two = {'Anna': 9}
merge_dictionaries(ages_one, ages_two) # { "Peter": 10, "Isabel": 11, "Anna": 9 }
merge_dictionaries
函数使用“可变参数”的形式接受多个字典,并返回合并后的字典对象。
update([other])
使用来自 other 的键/值对更新字典,覆盖原有的键。 返回None
。update()
接受另一个字典对象,或者一个包含键/值对(以长度为二的元组或其他可迭代对象表示)的可迭代对象。 如果给出了关键字参数,则会以其所指定的键/值对更新字典:d.update(red=1, blue=2)
。
版权说明 : 本文为转载文章, 版权归原作者所有 版权申明
原文链接 : https://www.cnblogs.com/felixz/p/15427486.html
内容来源于网络,如有侵权,请联系作者删除!