Python3 Introduction (iii) use of dictionaries

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.