python - 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 没有从函数调用中得到更新。
有人可以指出我正确的方向吗?
解决方案
对于加入字典,您可以简单地使用以下代码:
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}
推荐阅读
- c - getenv 返回受污染的字符串内容
- php - Wordpress:突出显示活动/选定的菜单项 CSS/PHP
- sqlite - SQLITE 3 不写入表
- c# - 旧版本的 MySQL 是否允许不同的日期格式?
- jquery - 带有日期范围的引导日期选择器结束/开始日期限制
- matplotlib - matplotlib 子图与 matshow
- python - python中的文本文件
- javascript - 在 Heroku 上构建之前如何在项目上运行 javascript 代码
- keras - Keras 模型嵌套在自定义 Keras 层内
- swift - 使用 MKLocalSearch 填充 UITableView