Python 字典 update() 方法用于更新字典中的键/值对,能够修改存在的键对应的值,也能够添加新的键/值对到字典中。html
用法与 Python dict() 函数类似。python
update() 方法语法:函数
D.update(key/value)
该方法没有任何返回值。htm
如下实例展现了 update() 方法的使用方法:blog
# !/usr/bin/python3 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) # 使用以上任意方法修改存在的键对应的值 print(D)
以上实例输出结果为:three
{'one': 1, 'three': 3, 'two': 2, 'four': 4} {'one': 1, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3} {'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'six': 6, 'two': 2, 'five': 5, 'three': 3} {'one': 1, 'eight': 8, 'seven': 7, 'four': 4, 'eleven': 11, 'six': 6, 'twelve': 12, 'two': 2, 'five': 5, 'three': 3} {'four': 4, 'seven': 7, 'twelve': 12, 'six': 6, 'eleven': 11, 'three': 3, 'one': 111, 'eight': 8, 'two': 222, 'five': 5}
zip() 函数:http://www.cnblogs.com/wushuaishuai/p/7766470.htmlip