Python3 Dict (dictionary) detailed

Source: Internet
Author: User
Clear (Empty dictionary contents)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ',    ' num3 ': ' Sam ',}print (Stu.clear ()) #输出: None

Copy (copy dictionary)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ',    ' num3 ': ' Sam ',}stu2 = Stu.copy () print (STU2)

Fromkeys (Specify a list, use the value in the list as the dictionary key, generate a dictionary)

name = [' Tom ', ' Lucy ', ' Sam ']print (Dict.fromkeys (name)) print (Dict.fromkeys (name,25))  #指定默认值 # output: {' tom ': None, ' Lucy ': None, ' Sam ': none}#     {' tom ': +, ' Lucy ': +, ' Sam ': 25}

Get (Specify key to get the corresponding value)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}print (Stu.get (' num2 ')) #输出: Lucy

Items (returns a list of elements that are made up of key-value pairs)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}print (Stu.items ()) #输出: Dict_items ([(' Num2 ', ' Lucy ') , (' num3 ', ' Sam '), (' Num1 ', ' Tom ')]

Keys (get all the keys in the dictionary)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}print (Stu.keys ()) #输出: Dict_keys ([' num3 ', ' num1 ', ' Num2 '])

Pop (Gets the value of the specified key and is deleted in the dictionary)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}name = Stu.pop (' num2 ') print (name,stu) #输出: Lucy {' Num1 ': ' Tom ', ' num3 ': ' Sam '}

Popitem (randomly get a key-value pair and delete it in the dictionary)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}name = Stu.popitem () print (Name,stu) #输出: (' num2 ', ' Lucy ') {' num3 ': ' Sam ', ' num1 ': ' Tom '}

SetDefault (Gets the value of the specified key, created if key does not exist)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}name = Stu.setdefault (' NUM5 ') print (name,stu) # Output: None {' num1 ': ' Tom ', ' num2 ': ' Lucy ', ' num5 ': None, ' num3 ': ' Sam '}

Update (add key-value pairs to dictionary)

Stu = {    ' num1 ': ' Tom ',    ' num2 ': ' Lucy ', '    num3 ': ' Sam ',}stu.update ({' num4 ': ' Ben '}) print (stu) #输出: {' num2 ': ' Lucy ', ' num3 ': ' Sam ', ' num1 ': ' Tom ', ' num4 ': ' Ben '}
Related Article

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.