A common data structure Python dictionary is provided, it is used to store data having a mapping relationship. For example, we have taken the results table data, language: 79 Math: 80 English: 92, looks like two lists, but there is a certain relationship between the elements of two lists this set of data. If you simply use two lists to save this set of data, it can not record the relationship between the two sets of data. Because the dictionary key data is critical, and the program needs to be accessed by key value, and therefore the dictionary key must be unique.
Programs can use curly braces syntax to create a dictionary, can also be used dict () function to create the dictionary. In fact, dict is a type that is in Python dictionary type.
When you create a dictionary to use curly braces syntax, braces should contain multiple key-value pairs, separated by a colon between the key and value; separated by commas between multiple key-value pairs.
= {A ' V1 ' : 12 is , ' V2 ' : 13 is , ' V3 ' : 14 , 12 is: 12 is } for X in A: # traversal key value Print ( " {} \ T \ T {} " .format (A [ " V1 " ], A [ " V2 " ]) ) for X in a.keys (): # cyclic output KEY Print (X) for X in a.values (): # cycle of the output value Print (X) for X, Y in a.items (): # cycles key-value pair Print (X, Y) a.pop ( ' V1 ' ) # delete the specified key corresponding to the key value of B = dict.fromkeys ([. 1, 2,. 3,. 4], 123) # eradicate sequence, to create the dictionary, and a uniform value Print (B) # {. 1: 123, 2: 123, . 3: 123,. 4: 123} Print (b.get ( ' 2 ' , 22222)) # If the acquired value is not developed element returns back fill: 22222 Print (b.pop (. 1)) # Delete the value of the specified element and stored in the POP Inside: 123 Print (b.pop ( ' 1 ' , 2222)) # If you can not find the time to develop delete elements, it will return later to fill in the value of Print (b.popitem ( )) # random key to delete a Print (b.setdefault ( ' . 1 ' ,. 9)) # set value exists, not set; the set value does not exist added Print (b.update ({. 1: 4444,. 5: 123 })) # update build, if not find the specified key will be added a.update (v1 = 123, v2 = 123) # update key (only update the string in the key, the merger of the two key figures do not allow update ) Print (A)
supplement:
del radiansdict [ ' the Name ' ] # delete key 'the Name' radiansdict.clear () # empty dictionary del radiansdict # delete dictionary len (radiansdict) # count the number of dictionary elements, i.e., the total number of keys. STR (radiansdict) # output dictionary, printable string representation. of the type (radiansdict) # returns the type of input variables, if the variable is a dictionary returns a dictionary. radiansdict.copy () # return a shallow copy of dictionary radiansdict.items () # to return may traverse the list (key, value) tuples array radiansdict.keys () # returns an iterator can be used list () to convert a list radiansdict.setdefault (Key, default = None)# And get () is similar, but if the key does not exist in the dictionary, it will add key and value to default radiansdict.update (dict2) # The dict2 dictionary of key / value pairs to the dict update in radiansdict.values () # returns an iterator can be used list () converts a list POP (key [, default]) # delete dictionary given key corresponding to the key value, the return value is deleted. key value must be given. Otherwise, return default values. popitem () # random back and delete the last pair of keys and values in the dictionary.