0
点赞
收藏
分享

微信扫一扫

.update()

eelq 2022-01-05 阅读 122
python

.update()方法详解

第一种:A.update(B):把字典B添加到字典A中

A= {'Name': 'Zara', 'Age': 7}
B = {'Sex': 'female' }

A.update(B)
print(A)

输出结果

{'Name': 'Zara', 'Age': 7, 'Sex': 'female'}

第二种:A.update(B):

用 update 更新字典 a,会有两种情况:
(1)有相同的键时:会使用最新的字典 b 中 该 key 对应的 value 值。
(2)有新的键时:会直接把字典 b 中的 key、value 加入到 a 中。

A = {1: 1, 3: 3}
B = {1: 2, 2: 2}

A.update(B)
print(B)

输出结果

{1: 2, 2: 2, 3: 3}

参考:https://www.runoob.com/python/att-dictionary-update.html

举报

相关推荐

0 条评论