Skip to content

Instantly share code, notes, and snippets.

@ayuLiao
Created June 25, 2019 11:06
Show Gist options
  • Save ayuLiao/990435c7a3469b71ddfcdbed3e96c75a to your computer and use it in GitHub Desktop.
Save ayuLiao/990435c7a3469b71ddfcdbed3e96c75a to your computer and use it in GitHub Desktop.
# 在 PEP448 中,有个新的语法可以实现,并且在 python3.5 中支持了该语法,合并代码如下
z = {**x, **y}
# 如果您还没有使用 Python 3.5,或者需要编写向后兼容的代码,并且您希望在单个表达式中运行,则最有效的方法是将其放在一个函数中
def merge_two_dicts(x, y):
"""Given two dicts, merge them into a new dict as a shallow copy."""
z = x.copy()
z.update(y)
return z
z = merge_two_dicts(x, y)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment