首页 > 解决方案 > 如何在 python 2.7 中合并两个以上的字典

问题描述

尝试在 python 2.7 中合并两个或多个字典,但是

** kwargs在 python 2.7 中不受支持,它会给出错误,如 Python 2.7 版不支持 dicts 中的星号表达式。

有没有办法在 python 2.7 中做到这一点?

# Create first dictionary
dict1 = {  'Ritika': 5, 'Sam': 7, 'John' : 10 }
# Create second dictionary
dict2 = {'Aadi': 8,'Sam': 20,'Mark' : 11 }
# Create second dictionary
dict3 = {'x': 8,'y': 20,'z' : 11 }

dict4 = {**dict1 , **dict2, **dict3}
print dict4

预期产出-

{'Ritika': 5, 'Sam': 20, 'John': 10, 'Aadi': 8, 'Mark': 11, 'x': 8, 'y': 20, 'z': 11}

标签: pythonpython-3.xpython-2.7dictionarymerge

解决方案


推荐阅读