抄来的
描述
Python 字典 update() 方法用于更新字典中的键/值对,可以修改存在的键对应的值,也可以添加新的键/值对到字典中。
用法与 Python dict() 函数相似。
D.update(key/value)
实例
以下实例展示了 update() 方法的使用方法:
D = {‘one’: 1, ‘two’: 2}
D.update({‘three’: 3, ‘four’: 4}) # 传一个字典
print(D)
D.update(five=5, six=6) # 传关键字
print(D)
D.update([(‘seven’, 7), (‘eight’, 8)]) # 传一个包含一个或多个元祖的列表
print(D)
D.update(zip([‘eleven’, ‘twelve’], [11, 12])) # 传一个zip()函数
print(D)
D.update(one=111, two=222) # 使用以上任意方法修改存在的键对应的值