The 1.dictionary:dictionary type is a dictionary that represents a key/value repository.
given a Dictionary object that searches for the value corresponding to the key, the dictionary is considered a list of key/value pairs. 2. Type definitionDictionary is the only class in Python that represents a mapping relationship, so it has its own unique definition and operating style. The form is as follows: {Key1:value,key2:value,...} 3. built-in functions Clear (): Clears all (key, value) pairs in the dictionary
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}user_info.clear ()Print(User_info)#{}
Copy (): Copies a copy of the dictionary.
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}user_info1=user_info.copy ()Print(USER_INFO1)#{' Gender ': ' M ', ' name ': ' Simon ', ' Age ': ' + '}
Fromkeys (Seq,val=none): Creates a dictionary with the elements in SEQ as keys, and all keys are set to val,val default to None
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}user_info1= User_info.fromkeys (["Address","School"],"Hangzhou")Print(USER_INFO1)#{' School ': ' Hangzhou ', ' address ': ' Hangzhou '}
Get (Key,default=none): reads the key in the dictionary, returns the value of the key, or returns the value set by default if it is not found.
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}d= User_info.get ('name',"Button") D1= User_info.get ('name1','Button')Print(d)#SimonPrint(D1)#Button
Items (): Returns a list that contains the tuple (key, value) in the dictionary.
keys (): Returns a list of all keys in a dictionary
VALUES (): Returns a list that contains all the values in the dictionary
User_info = { "name":"Simon", " Age":" -", "Gender":"M"} forKvinchUser_info.items ():Print(k, V)
Pop (Key[,default]): Reads the value of a key and removes the value of the key from the dictionary. Throws a Keyerror exception if key key does not exist and default is not set
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}user_info.pop ('name','Button')Print(User_info)#{' Gender ': ' M ', ' age ': ' + '}
SetDefault (Key,default=none): Sets the value of key in dictionary to default, like get ()
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}name= User_info.setdefault ("name1",'Button')Print(name)#ButtonPrint(User_info)#{' name ': ' Simon ', ' Gender ': ' M ', ' age ': ' + ', ' name1 ': ' button '}
Update (DICT): Merge dictionary (bulk Update dictionary)
User_info = { "name":"Simon", " Age":" -", "Gender":"M"}#Batch UpdateTest = { "A1":"123", "A2":"456", "A3":"789"}user_info.update (test)Print(User_info)#{' A2 ': ' 456 ', ' A1 ': ' 123 ', ' Gender ': ' M ', ' A3 ': ' 789 ', ' age ': ' + ', ' name ': ' Simon '}
4.del deleting elements
Test = { "A1":"123", "A2":"456", "A3":"789"}#Delete Elementdeltest["A1"]Print(test)#{' A2 ': ' 456 ', ' A3 ': ' 789 '}
5.pop () Deletes the element of the specified key
Test = { "A1":"123", "A2":"456", "A3":"789"} test.pop ("A1")Print(test)#{' a3 ': ' 789 ', ' A2 ': ' 456 '}
6.popitem () delete element from left
Test = { "A1":"123", "A2":"456", "A3":"789"}Print(test)#{' A1 ': ' 123 ', ' A2 ': ' 456 ', ' A3 ': ' 789 '}Test.popitem ()Print(test)#{' A2 ': ' 456 ', ' A3 ': ' 789 '}
A Dictionary of Python learning