我总是得到一本空字典
#!/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
有人能给我指出正确的方向吗
您可以使用
concatDicts
函数接受可变数量的dict作为输入,并返回新的合并dict对于加入词典,您只需使用以下代码:
结果:
您想用
dictCon
字典参数更新dictNew
。由于字典是可变的,您不需要保存或返回结果,因为dictNew
将发生变化:它给出:
但是请注意,您的函数实际上只是掩蔽
dictNew.update
,因此您最好使用该方法调用而不是此包装函数:另一种方法是使用
**
-运算符分解字典:相关问题 更多 >
编程相关推荐