One, the operation of the dictionary
a={'name':'wo',' Age': 18}dict.clear ()#Delete all elements in a dictionarya.clear () dict.copy ( )#returns a shallow copy of a dictionarya.copy () dict.get (key, default=none)#returns the value of the specified key if the value does not return the default value in the dictionaryA.get ('h','123') Dict.items ()#returns an array of traversed (key, value) tuples as a listPrint(A.items ())#ResultsDict_items ([('name','wo'), (' Age', 18)]) Dict.keys ()#returns a dictionary of all keys in a listPrint(A.keys ())#ResultsDict_keys ([' Age','name']) dict.values ()#returns all values in the dictionary as a listPrint(A.values ())#ResultsDict_values ([18,'wo']) Dict.fromkeys (seq[, Val] ))#Create a new dictionary with the keys to the dictionary in sequence seq, Val is the initial value corresponding to all keys in the dictionaryPrint([Dict.fromkeys],'Test'))#Results{1:'Test', 2:'Test', 3:'Test'}dict.setdefault (key, default=none)#similar to get (), but if the key does not exist in the dictionary, the key is added and the value is set to defaultA.setdefault ('s','123')Print(a)#Results{'s':'123','name':'wo',' Age': 18}dict.pop (Key[,default])#deletes the value of the dictionary given key key, and returns the value to be deleted. The key value must be given. Otherwise, the default value is returned. Print(A.pop ('123','s'))Print(a)#Resultss{' Age': 18,'name':'wo'}dict.popitem ()#randomly returns and deletes a pair of keys and values in a dictionaryPrint(A.popitem ())Print(a)#Results(' Age', 18){'name':'wo'}dict.update (Kwargs)#give the dictionary a new value, or you can pass in another dictionary, two dictionaries mergedA.update (sex='Mans')Print(a)#Results{'Sex':'Mans','name':'wo',' Age': 18}
Second, the traversal of the dictionary
a={'name':'wo','age': for in A.keys ()# traverse keyfor in A.values ( ) # traverse the values for in A.items ()# Traversal dictionary
Third, the existence or not judge
a={'name':'wo',' Age': 18}'name' inchA#determine if key is in the dictionary'name' inchA.keys ()#judging = whether in key'name' inchA.values ()#Judging whether in the values
Operation of the Python dictionary