Dictionary features: Dictionary keys are unique, the dictionary is unordered can not be the subscript to take the value, through key to take the following examples of the dictionary related usage names={' zhu ': [, ' NV ', ' Shijiazhuang '], ' Yue ': {' age ': '}}--- The value of the dictionary can be a list, multiple kv separated by commas one, values: Names.get (' Zhu ')---does not exist return nonenames[' Zhu ')---does not exist will be error two, add names[' yue1 ']=[18,1993] three, Change names[' Zhu ']=[19] IV: Delete 1, del names[' Zhu '] 2, Names.pop (' Zhu ')---Delete the specified Key3, Names.popitem ()---Randomly delete a value five, built-in method: names={" Zhu ":" NV "}ages={" Age ": 18}names.keys ()---Print all key values Names.values ()---Print all value values Names.setdefault (' Juan ', [ 18,19931116])---Set key existence does not modify the original value, does not exist the new, the parameter is (key,vaule)--vaule optional, not written as Nonenames.update (ages)--- Add the dictionary ages to the dictionary names---Extended DictionaryNames.has_key (' Zhu ')----Determine if there is this keyThis method exists in Python2' Zhu ' in names---determine if key is Names.items () in the dictionary---convert the dictionary to a list to get the values of key and values in the dictionary, the dictionary will be converted to a list for k,v in Names.items ()---deprecated Print (k,v) get keyfor K in Names () print (k) Get key and values, still the way of the dictionary-more efficient for K in Names:print (K,names[k])
Python---Dictionary-related operations