Dictionary common method of exercise
. 1 # Coding: UTF. 8- 2 . 3 # dictionary common way to practice . 4 . 5 # create the dictionary two methods . 6 dict1 = { " K1 " : " V1 " , " K2 " : " V2 " } . 7 dict2 dict = (K3 = " v3 " , K4 = " V4 " ) 8 Print ( " first create results: " , dict1) 9 Print ( " The second result is to create: " , dict2) 10 . 11 # within clearing the dictionary are all the elements 12 is Print ( " dict2 the dictionary is clear: " , End = "" ) 13 is Print (dict2.clear ()) 14 15 # dictionary replication 16 dict_copy dict1.copy = () # shallow copy 17 Print ( " dict_copy is copied from dict1 over: " , End = "" ) 18 Print (dict_copy) 19 20 # created with the incoming sequence and the value of the new dictionary, the value of optional, defaults to None 21 dict_new = dict.fromkeys (( " A1 " , "A2 " , " A3 " ),. 5 ) 22 is Print ( " using the new dictionary is created fromkeys: " , dict_new) 23 is 24 # Two Ways by key value: 25 Print ( " using the acquired array subscript: " , dict1 [ " K1 " ]) 26 is Print ( " using the method to get get: " , dict1.get ( " K1 " )) 27 # difference between the two methods: using the index acquisition, throwing the system out of range abnormality index, use get direct return None 28 # Print ( "subscript cross-border acquisition:", dict1 [ "k3"]) 29 print( " Subscript out of range acquisition: " , dict1.get ( " K3 " )) 30 31 is # acquires dictionary all keys, values and values of 32 Print ( " all keys: " , dict1.keys ()) # dict_keys ( [ 'K1', 'K2']) 33 is Print ( " all values: " , dict1.values ()) # dict_values ([ 'V1', 'V2']) 34 is Print ( " All key-value pairs: " , dict1 .items ()) # dict_items ([( 'K1', 'V1'), ( 'K2', 'V2')]) 35 # applications 36 for K, Vin dict1.items (): 37 [ Print ( " bond is: " , K, " is: " , V) 38 is 39 # Delete the specified key-value pairs, at least one parameter transmission: 40 dict1.pop ( " K1 " ) 41 is Print ( " key is deleted after the k1: " , dict1) 42 is 43 is # randomly delete a key for the dictionary, but the individual test N times, are deleted "d": "4". . . There is no explanation for the god 44 is dict3 = { " A " : " . 1 " , " B " : ", " C " : " . 3 " , " D " : " . 4 " } 45 Print ( " not previously used popitem Delete: " , dict3) 46 is dict3.popitem () 47 Print ( " after-erase popitem: " , dict3) 48 49 # lookup value corresponding to the specified key, the return value is present, it does not exist to create and assign default values 50 Print ( " key to find the presence of: " , dict3.setdefault ( " a " )) 51 is Print(dict3.setdefault ( " the original key does not exist " , " Find finish was added to it a " )) 52 # change view dict3 of 53 Print ( " dict3 changes: " , dict3) 54 55 dict4 = { " name " : " Lee " , " Age " : 12 is } 56 is dict5 = { " Sex " : " man "} 57 # using the update implement updates to existing content dictionary 58 Print ( "Before being dict4 Update: " , dict4) 59 dict4.update ({ " Age " : 20 is}) # Note that the parameters passed to the dictionary 60 Print ( " after being dict4 update (update data): " , dict4) 61 is dict4. update (dict5) 62 is Print ( " the updated content is not available dictionary, for the add operation: " , dict4)