The Dict () function can be created by other mappings or by a sequence (key, value) such as a dictionary//dict is not a function, it is the same type as List tuple str
Items = [(' Name ', ' Gumy '), (' Age ', 20)]
D = dict (items)
or D = dict (name = ' Gumy ', age = 20)
Basic Dictionary Operations
Len (d) The number of key-value pairs in translation dictionary D
D[K] Returns the value on the key K
D[k] = V associates the value V to the key K
Del D[k] Delete item with key K
K in D check if there is a key k in D
formatting strings for Dictionaries
Phonebook = {' Beth ': ' 9102 ', ' Alice ': ' 2341 ', ' Cecil ': ' 3253 '}
"Alice ' s phone is% (Alice) S"% phonebook
The method of the dictionary
Clear () Clears all items in the dictionary
Copy () Shallow copy
Deepcopy () deep copy
Fromkeys () using the given key CV dictionary, each key default value bit None
{}.fromkeys ([' Name ', ' age ']) is equivalent to Dict.fromkeys ([' Name ', ' age '])
Has_key detect if a given key is included in the dictionary
Items returns all dictionary items in a list, with no special order
Iteritems is similar to items except that it returns an iterator, which is then listed using the list method:
it = D.iteritems ()
List (IT)
The Keys method returns the key as a dictionary, while Iterkeys returns an iterator for the key
Pop deletes key value pairs based on key value
D = {' X ': 1, ' Y ': 2}
D.pop (' x ')
The Popitem method, similar to pop, uses this method if it is deleted in order of one after another, and the method does not have to enter a key value
The Get method is similar to the SetDefault method where the given key value obtains the value associated with the key, and for the first time, the SetDefault can also set the corresponding key value without the given key in the dictionary.
Update will be added to the old dictionary based on the entries in the supplied dictionary, and will overwrite if the same key is available.
The values method returns the value in the dictionary as a list, and Itervalues returns a worthwhile iterator.
Basic Python Tutorial (chapter fourth)