如何在Python3.5+中用一个表达式合并两个字典dict
>>> x = {'a': 1, 'b': 2}
>>> y = {'b': 3, 'c': 4}
>>> z = {**x, **y}
>>> z
{'c': 4, 'a': 1, 'b': 3}
在Python2里应该这样写
>>> z = dict(x, **y)
>>> z
{'a': 1, 'c': 4, 'b': 3}
[title]关于两个星号(**)的说明:[/title]
双星号(**)将参数以字典的形式导入:
def bar(param1, **param2): print (param1) print (param2) bar(1,a=2,b=3)
以上代码输出结果为:
1
{'a': 2, 'b': 3}
详细解释可参考:http://www.runoob.com/w3cnote/python-one-and-two-star.html