首页 > 解决方案 > python3连接python字典的函数不起作用

问题描述

我不断得到一本空字典

#!/usr/local/bin/python3


dic1={1:10, 2:20}
dic2={3:30, 4:40}
dic3={5:50,6:60}
dictNew = {}


def concatDict(dictCon):
    dictNew = dict.update(dictCon)
    return dictNew


concatDict(dic1)
concatDict(dic2)
concatDict(dic3)

print(dictNew)

dictNew 没有从函数调用中得到更新。

有人可以指出我正确的方向吗?

标签: pythondictionary

解决方案


对于加入字典,您可以简单地使用以下代码:

dict1 = {1: 10, 2: 20}
dict2 = {3: 30, 4: 40}
dict3 = {5: 50, 6: 60}
dict_new = {**dic1, **dic2, **dic3}
print(dict_new)

结果:

{1: 10, 2: 20, 3: 30, 4: 40, 5: 50, 6: 60}

推荐阅读