The basic use of the update of the python

The basic use of the update of the python

Python dictionary update () method for updating dictionary key / value pairs can be modified corresponding to the key value exists, and can also add a new key / value pairs to the dictionary.

Syntax

d.update(e)

Parameter Description
The key e, - Value of d is added to the dictionary, the dictionary may be e, it may be the key - the value of the sequence. See examples.

Return Value
This method does not have any return value.

Examples
The following example shows how to use the update () method:

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(([‘nice’,9],[‘ten’,10]))#传一个包含一个或多个列表的元组 
print(d)

d.update(zip([‘eleven’,’twelve’],[11,12])) # 传一个zip()函数 
print(d)

d.update(one=111,two=222) # 使用以上任意方法修改存在的键对应的值 
print(d)

Examples of the above output is:

{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘two’: 2} 
{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6} 
{‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘five’: 5, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
{‘seven’: 7, ‘one’: 1, ‘four’: 4, ‘three’: 3, ‘ten’: 10, ‘five’: 5, ‘nice’: 9, ‘two’: 2, ‘six’: 6, ‘eight’: 8} 
{‘one’: 1, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 2, ‘nice’: 9, ‘five’: 5, ‘eight’: 8} 
{‘one’: 111, ‘four’: 4, ‘three’: 3, ‘twelve’: 12, ‘ten’: 10, ‘seven’: 7, ‘six’: 6, ‘eleven’: 11, ‘two’: 222, ‘nice’: 9, ‘five’: 5, ‘eight’: 8}

in python dictionary (dict) Other operations See below for the link to view:
https://blog.csdn.net/qq_44401643/article/details/89165557

Guess you like

Origin www.cnblogs.com/chunbo/p/11198808.html