Common dictionaries for Python are denoted by {} curly braces.
Dict1 = {key1:value1, key2:value2}
Each key value of the dictionary is key:value corresponding to the colon, and each key value is separated by commas
A dictionary definition method, such as
A={name:huang}
Print a
Name is Key,huang is value,
B=dict (a=1,b=2)
A, B is key,1,2 is value
c= ([' Name ': ' Huang '), (' Age ': ' 20 ')])
This means that you can also
Common ways of working with dictionaries:
. Get () Gets the value corresponding to key
a={"name": "Huang", "Age": "20"}
M=a.get ("Age")
Print (m)
Then the return value is 20.
If the value does not exist, the default value of None is returned
M=a.get ("Address")
Then the return value is none.
SetDefault () similar to get if the value exists returns the corresponding value, there is no return setting
a={"name": "Huang", "Age": "20"}
M=setdefault ("Age", 50)
Print (' m ')
Then the return value is 20, which corresponds to the age of the VALUE20
If
M=setdefault ("Addree", 50)
Print (' m ')
Then the return value is 50.
Key () gets all the keys
Value () gets all the value
Items () Traverse all keys and value
a={"name": "Huang", "Age": "20"}
For key,values in A.items ()
Print Key,values
The output is as follows:
Name Huang
Age 20
The difference between Python2 and Iteritems () is that the items are similar to the book's outline, iteritems similar to the content of the book, when loading the outline than content loading faster
Update () Merge dictionary
a={"name": "Huang"}
B={"Age": 20}
A.update (b)
Print (a)
Merge B to a, the result output is
{"Name": "Huang", "Age": "20"}
Pop () deletes the value corresponding to the given key and returns the deleted value
a={"name": "Huang", "Age": "20"}
B=a.pop ("name")
Print (b)
POPs are output.
Remove the value for Nam, the return value is ' Huang '
Copy () replication
a={"name": "Huang", "Age": "20"}
B=a.cpoy
Print (b)
{"Name": "Huang", "Age": "20"}
High-order function zip
A=[name,age]
B=[HUANG,20]
Zip (A, B)
Output to
[(Name,huang), (age,20)]
Upper and lower one by one correspondence
Python3 Introduction (iii) use of dictionaries