Python 3.0 dictionary Additions and deletions

Source: Internet
Author: User

First, the definition of the dictionary method:

1. dic = {' name ': ' Karen ', ' age ': $, ' hobby ': ' Girl ', ' Is_handsome ': True}

Print (DIC) #==>{' name ': ' Karen '}

DIC = {' name ': ' Karen ', ' age ': $, ' hobby ': {' name ': ' Xu ', ' Age ': $}, ' Is_handsome ': True}

2, Dic=dict ((' Name ', ' Karen '),) | |   Dic=dict ([' Name ', ' Karen '],)) | |      Dic=dict ([[' Name ', ' Karen '],]) | | Dic=dict ([' Name ', ' Karen '),])

Print (DIC) #==>{' name ': ' Karen '}

Two main features of the dictionary: disorder, key unique

Dictionary keys can only have immutable elements >>>>> immutable types: Integer, string, tuple mutable type: list, dictionary

Second, increase

1. dic1={' name ': ' Karen '}

dic1[' age ']=18

Print (DIC1) #==>{' name ': ' Karen ', ' Age ': 18}

2, Dic1.sendefault (' Age ', "#如果有这个键值对"), do nothing and return the value, if not, increase and return the value

Print (DIC1)

Third, find

1. dic={' name ': ' Karen ', ' Age ': 18}

Print (dic[' name ']) #==>karen

2, Print (Dic.keys ()) #==> only printing keys

The type is Dict_keys type, not a list

Print (Dic.values ()) #==> prints only values

Print (Dic.items ()) #==> printing key-value pairs

Iv. Change of

1. dic={' name ': ' Karen ', ' Age ': 18}

dic[' age ']=33

Print (DIC) #==>{' name ': ' Karen ', ' Age ': 33}

2. dic={' name ': ' Karen ', ' Age ': 18}

Dic1={' a ': ' AAA ', ' B ': ' BBB '}

Dic.update (DIC1) #若有值则替换

V. Delete

1. dic={' name ': ' Karen ', ' Age ': 18}

del dic (' name ')

Print (DIC) #==>dic={' age ': 18} Delete entire key-value pairs

2, Dic.clear ()

Print (DIC) #==>dic={}

3. Dic.pop (' age ')

Print (DIC) #==>dic={' age ': 18} Deletes the entire key-value pair and returns the value as a return value

4, A=dic.popitem ()

Print (A,dic) #随机删除

Vi. other operations and the methods involved

1, Dic=dict.fromkeys ([' host1 ', ' host2 ', ' host3 '], ' test ')

Print (DIC) #==>{' host1 ', ' host2 ', ' host3 ', ' Test '} Initialize dictionary with keys, no value

2, Dic=[5: ' 555 ', 2: ' 666 ', 4: ' 444 ']

Print (sorted (Dic.items ()))

Print (sorted (Dic.keys ()))

Print (sorted (dic.values ()))

3. dic={' name ': ' Karen ', ' Age ': 18}

For I in DIC:

Print (I,dic[i])

Python 3.0 dictionary Additions and deletions

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.