Clear (Empty dictionary contents)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}print (Stu.clear ()) #输出: None
Copy (copy dictionary)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}stu2 = Stu.copy () print (STU2)
Fromkeys (Specify a list, use the value in the list as the dictionary key, generate a dictionary)
name = [' Tom ', ' Lucy ', ' Sam ']print (Dict.fromkeys (name)) print (Dict.fromkeys (name,25)) #指定默认值 # output: {' tom ': None, ' Lucy ': None, ' Sam ': none}# {' tom ': +, ' Lucy ': +, ' Sam ': 25}
Get (Specify key to get the corresponding value)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}print (Stu.get (' num2 ')) #输出: Lucy
Items (returns a list of elements that are made up of key-value pairs)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}print (Stu.items ()) #输出: Dict_items ([(' Num2 ', ' Lucy ') , (' num3 ', ' Sam '), (' Num1 ', ' Tom ')]
Keys (get all the keys in the dictionary)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}print (Stu.keys ()) #输出: Dict_keys ([' num3 ', ' num1 ', ' Num2 '])
Pop (Gets the value of the specified key and is deleted in the dictionary)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}name = Stu.pop (' num2 ') print (name,stu) #输出: Lucy {' Num1 ': ' Tom ', ' num3 ': ' Sam '}
Popitem (randomly get a key-value pair and delete it in the dictionary)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}name = Stu.popitem () print (Name,stu) #输出: (' num2 ', ' Lucy ') {' num3 ': ' Sam ', ' num1 ': ' Tom '}
SetDefault (Gets the value of the specified key, created if key does not exist)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}name = Stu.setdefault (' NUM5 ') print (name,stu) # Output: None {' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num5 ': None, ' num3 ': ' Sam '}
Update (add key-value pairs to dictionary)
Stu = { ' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num3 ': ' Sam ',}stu.update ({' num4 ': ' Ben '}) print (stu) #输出: {' num2 ': ' Lucy ', ' num3 ': ' Sam ', ' num1 ': ' Tom ', ' num4 ': ' Ben '}