首页 > 编程语言 > 详细

python dict update函数

时间:2021-02-22 12:05:36      阅读:25      评论:0      收藏:0      [点我收藏+]

 

Python 字典(Dictionary) update() 函数把字典dict2的键/值对更新到dict里。

dict.update(dict2)
  • dict2 -- 添加到指定字典dict里的字典。
  • 该方法没有任何返回值。
dict = {Name: Zara, Age: 7}
dict2 = {Sex: female }

dict.update(dict2)
print("Value : %s" %  dict)
#Value : {‘Name‘: ‘Zara‘, ‘Age‘: 7, ‘Sex‘: ‘female‘}

 

用 update 更新字典 a,会有两种情况:

  •  (1)有相同的键时:会使用最新的字典 b 中 该 key 对应的 value 值。
  •  (2)有新的键时:会直接把字典 b 中的 key、value 加入到 a 中。
a = {1: 2, 2: 2}
b = {1: 1, 3: 3}
a.update(b)
print(a)
#{1: 1, 2: 2, 3: 3}

 

python dict update函数

原文:https://www.cnblogs.com/cgmcoding/p/14428683.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!