Create dictionary used in two ways:
Method One: Method key-value pairs:
1 my_dict={"key1":"value1","key2":"value2","key3":"value3" ……}
my_dict = { " name " : " Bob " , " Age " : 20 is, " Sex " : " M " } Print (my_dict)
output format is as follows:
{ 'name': 'Bob', 'age': 20, 'sex ':' M '}
Method Two: Create a dictionary by mapping function,
list1: a list that specifies the key to be generated dictionary
list2: a list to specify the value to be generated dictionary
Return Value: If the length of list1 and list2 different, is the same as the shortest length of the list
Another way to write the dictionary:
Here element 135 represents the key value of the element 246 is a value representative of the value
my_dict = dict (((element 1, element 2), (3 element, the element 4), (elements 5, 6 elements) ......))
my_dict = dict(zip(list1, list2))
1 # create a dictionary by a mapping function, 2 . 3 name = [ " Duncan " , " Ginobili " , " Parker " ] . 4 Sign = [ " Buddha " , " Yao Dao " , " sports " ] . 5 . 6 # Dictionary = dict (ZIP (List1, List2)) . 7 # output format is the format of the key . 8 my_dic = dict (ZIP (name, Sign)) . 9 Print (my_dic) 10
output format is as follows:
· { 'Duncan': 'Buddha', 'Ginobili': 'Yao Dao', 'Park': 'car'}
11 # Output is: a list format, the list of tuples, where a tuple is string 12 is # List List = (ZIP (List1, List2)) 13 is my_dict = List (ZIP (name, Sign)) 14 Print (my_dict)
Output format is as follows:
[( 'Duncan', 'Buddha'), ( 'Ginobili', 'Yao Dao'), ( 'Park', 'sports')]
Create dictionary a null value:
Use dict object fromkeys () method to create the dictionary is empty,
. 1 NAME_LIST = [ " Duncan " , " Ginobili " , " Parker " ] 2 Dictionary = dict.fromkeys (NAME_LIST) . 3 Print (Dictionary)
output format is as follows:
{ 'Duncan': None, 'Ginobili' : None, 'Park': None}
Create dictionary by existing tuples and lists:
. 1 name_tuple = ( " Duncan " , " Ginobili " , " Parker " ) 2 Sign = [ " Buddha " , " Yao Dao " , " sports " ] . 3 dict1 = {name_tuple: Sign} . 4 Print (dict1)
Output The results are as follows:
{( 'Duncan', 'Ginobili', 'Park'): [ 'Buddha', 'Yao Dao', 'sports']}
Delete dictionary: (dictionary as a dictionary of name)
Delete the entire dictionary:
del dictionary
Delete all of the elements of the dictionary
dictionary.clear()
Removes and returns the specified element "Key" (specify the key to delete the dictionary)
dictionary.pop ( "dictionary" key "")
. 1 my_dict = { " name " : " Bob " , " Age " : 20 is, " Sex " : " M " } 2 Print (my_dict)
. 3 my_dict.pop ( " Age " ) . 4 Print (my_dict)
output:
{ 'name': 'Bob', 'age': 20, 'sex': ' M'}
{ 'Name': 'Hsiao Ming', 'sex': 'man'}
Remove and return an element dictionary
dictionary.popitem()
= {my_dict " name " : " Bob " , " Age " : 20 is, " Sex " : " M " } Print (my_dict) # outputting the dictionary and returns the dictionary delete key to delete the RET = my_dict.popitem () Print (RET) # result output is: # ( 'Sex', 'M')
# after deleting the remaining output dictionary elements my_dict.popitem () Print (my_dict) # result output is: # { 'name': 'Bob ',' age ': 20}
Gets the element you want the dictionary:
Method 1: "key" element values to obtain the desired (do not recommend using this method, if the acquisition by key values, key value does not exist, the program will report an exception)
Method two: The method of obtaining the value of the key by the get ()
. 1 name = [ " Duncan " , " Ginobili " , " Parker " ] 2 Sign = [ " Buddha " , " Yao Dao " , " sports " ] . 3 my_dict = dict (ZIP (name, Sign)) . 4 # Print (my_dict) . 5 # returns the value of the default . 6 Print ( " Ginobili nickname is: " , my_dict.get ( " Ginobili ")) 7 # case key value does not exist in return: "My dictionary does not this person." 8 Print ( " Robinson's nickname is: " , my_dict.get ( " Robinson " , " my dictionary there is no person " ))