First, create a dictionary
note:
Each key value separated by a colon (:), each pair with a comma, each pair separated by commas, the whole in curly braces ({}).
Key must be unique, but the value is not necessary.
Type of data may take any value, but must be immutable, such as a string, or the number of tuples.
dict = { ' Alice ' : ' 2341 ' , ' Beth ' : ' 9102 ' , ' Cecil ' : ' 3258 ' } dict1 = {} # empty dictionary # may thus created dictionary dict1 = { ' ABC ' : 456 } dict2 {= ' ABC ' : 123, 98.6: 37 [}
Second, access to the dictionary values
Note: The appropriate key into the familiar square brackets, the following examples:
dict = { ' the Name ' : ' Zara ' , ' Age ' :. 7, ' Class ' : ' First ' } Print ( " dict [' the Name ']: " , dict [ ' the Name ' ]) Print ( " dict [' : Age '] " , dict [ ' Age ' ]) # examples of output results of the above: # dict [' the Name ']: Zara # dict [' Age ']:. 7
If there are no keys to access the data dictionary, it will output the following error:
dict = { ' the Name ' : ' Zara ' , ' Age ' :. 7, ' Class ' : ' First ' } Print ( " dict [ 'Alice']: " , dict [ ' Alice ' ]) # above example output: # KeyError: 'Alice'
Third, modify the dictionary
Note: add new content to the dictionary method is to add a new key / value pairs, modify, or delete existing key / value pairs in the following examples:
dict = {'Name': 'Zara', 'Age': 7, 'Class': 'First'} dict['Age'] = 8; # update existing entry dict['School'] = "DPS School"; # Add new entry print ("dict['Age']: ", dict['Age']) print ("dict [ 'School']: " , dict [ ' School ' ]) # examples of output results of the above: # dict [ 'Age']:. 8 # dict [ 'School']: the DPS School
Fourth, delete / remove dictionaries and dictionary content
Note: can delete a single element can be empty dictionary, emptied just one operation.
Delete a dictionary with the del command, the following examples:
= {dict ' the Name ' : ' Zara ' , ' Age ' :. 7, ' Class ' : ' First ' } del dict [ ' the Name ' ] # delete key is 'Name' entry dict.clear () # Clear all dictionaries entries KeyError: 'Age' del dict # delete dictionary TypeError: 'of the type' Object IS not subscriptable Print ( " dict [ 'Age']: " , dict [ ' Age ']) # But this raises an exception, because there is no longer with the del dictionary
Five key characteristics of the dictionary
note:
Dictionary without limitation value may take on any python object may be a standard object, or may be user-defined, but not the key.
Two important points to remember:
1) does not allow the same key appears twice. When you create is assigned if the same key twice, the last value will be remembered, the following examples:
dict = { ' the Name ' : ' Zara ' , ' Age ' :. 7, ' the Name ' : ' Manni ' } Print ( " dict [ 'the Name']: " , dict [ ' the Name ' ]) # above example output: # dict [ 'the Name']: Manni
2) key must not be changed, it is possible to use a number, string, or act as a tuple, it will not work with the list, the following examples:
dict = {[ ' the Name ' ]: ' Zara ' , ' Age ' :. 7 } Print ( " dict [ 'the Name']: " , dict [ ' the Name ' ]) # above example output: # TypeError: unhashable type: 'list'
Sixth, the dictionary built-in method / function
Python dictionary contains the following built-in functions:
cmp (dict1, dict2) # compare two elements in the dictionary. len (dict) # count the number of dictionary elements, i.e., the total number of keys. STR (dict) # string dictionary printable output representation. of the type (variable) # returns the type of input variables, if the variable is a dictionary returns a dictionary. radiansdict.clear () # delete all the elements in the dictionary radiansdict.copy () # returns a shallow copy of the dictionary radiansdict.fromkeys () # create a new dictionary, a sequence of elements do seq dictionary key, val is a dictionary of all key corresponds the initial value radiansdict.get (key, default = None) # returns the value specified key, if the value is not in the dictionary returns default value radiansdict.has_key (key) # If the key is in the dictionary dict returns true, otherwise returns false radiansdict.items () # to return may traverse the list (key, value) tuples array radiansdict.keys () # to return a list of all of the dictionary keys radiansdict.setdefault (Key, default = None) # and get () is similar, but if the key does not already 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 () # to return a list of all the values in the dictionary
Seven, to determine whether there is a dictionary of key
note:
Generally, there are two general approaches:
The first method: using the native functions implemented:
Inside dictionary python has_key method has a property () Method:
# Generate a dictionary D = { ' name ' : Tom, ' Age ' : 10, ' Tel ' : 110 } # print the return value Print D.has_key ( ' name ' ) # results return True
The second method: Use in Methods: In addition to use in can also be used not in, we determined that this key does not exist, in faster than has_key.
# Generate a dictionary D = { ' name ' : Tom, ' Age ' : 10, ' Tel ' : 110 } # print the return value, which d.keys () list all dictionaries Key Print 'name' in D. Keys () Print ' name ' in D # two results are returned True
Eight, the dictionary key, value replacement value date
h = '' hh = 0 hhh = 130255555555555555 lis = {} lis.update(h1=h,h2=hh,h3=hhh) for i, values in lis.items(): if i == 'h1': if values == '' or values == 0: lis.update(h1='张三') elif i == 'h2': if values == '空' or== values 0: lis.update (H2 =. 1 ) elif I == ' H3 ' : IF values == ' empty ' or values == 0: lis.update (H3 = 666,666,666,666,666 ) the else : Print ( " no " ) Print (LIS) # result { ' h1 of ' : ' John Doe ' , ' H2 ' :. 1, ' H3': 130255555555555555}