Advertisement
Guest User

Untitled

a guest
Oct 18th, 2019
114
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.50 KB | None | 0 0
  1. #以下方法可用于合并两个词典。
  2.  
  3. def merge_two_dicts(a, b):
  4. c = a.copy() # make a copy of a
  5. c.update(b) # modify keys and values of a with the ones from b
  6. return c
  7. a = { x : 1, y : 2}
  8. b = { y : 3, z : 4}
  9. print(merge_two_dicts(a, b)) # { y : 3, x : 1, z : 4}
  10.  
  11. #在Python 3.5及更高版本中,你还可以执行以下操作:
  12.  
  13. def merge_dictionaries(a, b)
  14. return {**a, **b}
  15. a = { x : 1, y : 2}
  16. b = { y : 3, z : 4}
  17. print(merge_dictionaries(a, b)) # { y : 3, x : 1, z : 4}
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement