Dictionary (dictionary): dictionary definition using {}, curly braces, each value separated by ",", Key and value using ":".
Dict1 = {'Tom'amercia'Jerry' ' Amercia '] ,'jack'England']}
Features of the dictionary:
The dictionary is unordered because it has no subscript and is indexed with key, so it is unordered
The key of the dictionary must be unique because it is indexed by key, so key cannot be duplicated and is inherently heavy.
Some operations of the dictionary
add element
1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3dict1['Rose'] = [21,'England']
modifying elements
Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}dict1['Tom'] = [14,'Amercia']
Delete Element
1 Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 Dict1.pop ('Jerry')#Delete the specified element3 delDict1 ('Jerry')#use the Del method to delete
Querying elements
1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3 Print(Dict1.get ('Jack'))#this way, if key does not exist, it will return none.4 5 Print(dict['Jack'])#this way, if key does not exist, it will be an error .6 7 Print('Jack' inchDICT1)#determine if Jack is in this dictionary, returns TRUE or false
Built-in methods
1Dict1 = {'Tom': [12,'Amercia'],'Jerry': [11,'Amercia'] ,'Jack': [20,'England']}2 3 Print(Dict1.keys ())#Print all keys4 5 Print(Dict1.values ())#Print all value6 7 Print(Dict1.setdefault. ('Judy', [20,'England']))#If key exists, does not move, does not exist, add8 9Dict2 = {'John': [24,'Russia']}Ten OneDict1.update (DICT2)#Update dictionary, if key exists, update; not present, add
Print (Dict1.items ()) #转变成列表
A Dictionary of Python