1Dic1 = {'name':'Paoche',2 ' Age': 19,3 'Active':['Beijing','Hebei'],4 'ID':{'Wangyi':'skmdod008b',5 'Sina':'SKMDOD00BC'6 }7}One, the dictionary increment Method 1: The direct new key value pair, if does not have this key to add the key value pair, has this key to modify the value
1dic1[' High'] = 180#no key-value pairs, append2dic1[' Age'] = 30#If there is a key value, modify3 Print(DIC1)4 """5 The result is:6 {' name ': ' Paoche ', ' age ': +, ' active ': [' Beijing ', ' Hebei '], ' id ': {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}}
7 {' name ': ' Paoche ', ' age ': +, ' active ': [' Beijing ', ' Hebei '], ' id ': {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}, ' H IgH ':8 """Method 2: Through the SetDefault () method, if the key is not the new key value pair, there is no action for this key
1Dic1.setdefault ('Weight', 185)#no key-value pairs, append2Dic1.setdefault ('name','Xiaobai')#There are key values, then no action is done3 Print(DIC1)4 """5 The result is:6 {' name ': ' Paoche ', ' age ': +, ' active ': [' Beijing ', ' Hebei '], ' id ': {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}, ' W Eight ': 185}7 """Second, the dictionary deletion Method 1: Use the Pop () method, the key to delete, return the value of the deleted key value pairs, no corresponding key error, you can set the value returned when no key
1 Print(Dic1.pop ('ID'))#Key Delete, return the value of the deleted key value pair, no corresponding key error2 Print(Dic1.pop ('ID', None))#Key Delete, if not, returns the specified value, generally returns none, or can be any content3 Print(DIC1)4 """5 The result is:6 {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}7 None8 {' name ': ' Paoche ', ' age ': +, ' active ': [' Beijing ', ' Hebei ']}9 """Method 2: Use the Popitem () method to randomly delete a key-value pair, returning a tuple that consists of deleted key values
1 Print (Dic1.popitem ()) # random Delete, returns a tuple of deleted key values 2 """ 3 The result is: 4 (' id ', {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}) 5 """
Method 3: Use the Clear () method to empty the dictionary with a return value of None
1 Print (Dic1.clear ()) # return None 2 Print (DIC1) 3 """ 4 The result is: 5 None 6 {}7 "" "
Method 4: Use Del to delete the specified key-value pair (no key is an error), or delete the entire dictionary
1 del dic1['ID']2print(dic1)3 del Dic1 4 """ 5 The result is: 6 {' name ': ' Paoche ', ' age ': +, ' active ': [' Beijing ', ' Hebei ']} 7 """
Three, the Dictionary modification Method 1: Through the key, directly changes the value
1 dic1['age'] = 30
Method 2: Use the update () method
1Dic2 = {2 'name':'Paoche',3 ' Age': 194 }5DIC3 = {6 ' Age': 30,7 'Weight': 1808 }9Dic2.update (DIC3)#The data in the DIC3 is updated dic2, there is a change, there is no new addTen Print(DIC2) One """ A The result is: - {' name ': ' Paoche ', ' age ': +, ' weight ': +} - """
Four, the dictionary search method 1: Directly through the key query value, without the key error
1 Print (dic1['age') 2 " " 3 The result is: 4 + 5 " "
Method 2: Use the Get () method to query the value of the key, return the value, return none without the key, set the return value
1 Print(Dic1.get (' Age'))2 Print(Dic1.get ('Age1'))3 Print(Dic1.get ('Age1','there are no corresponding keys.'))4 " "5 The result is:6 +7 None8 there are no corresponding keys.9 " "Method 3: Use the keys () method to query all keys to return to the list
1 Print (Dic1.keys ()) 2 " " 3 The result is: 4 Dict_keys ([' Name ', ' age ', ' active ', ' id ']) 5 " "
Method 4: Use the values () method to query all values and return a list of all values
1 Print (Dic1.values ()) 2 " " 3 The result is: 4 dict_values ([' Paoche ', +, [' Beijing ', ' Hebei '], {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '}]) 5 " "
Method 5: Use the items () method, get all the key-value pairs, return the list, and each element is a tuple of key-value pairs
1 Print (Dic1.items ()) 2 " " 3 The result is: 4 dict_items ([' Name ', ' Paoche '), (' age ', +), (' Active ', [' Beijing ', ' Hebei ') '), (' id ', {' Wangyi ': ' skmdod008b ', ' Sina ': ' SKMDOD00BC '}])5"
The list of items () can be assigned directly to the variable using the
1 for inch Dic1.items (): 2 Print (k,v) 3 " " 4 The result is: 5 name Paoche 6 Age 7 active [' Beijing ', ' Hebei '] 8 ID {' Wangyi ': ' skmdod008b ', ' sina ': ' SKMDOD00BC '} 9 " "
Python Learning-dictionary additions and deletions