Python dictionary operations

Source: Internet
Author: User

1. Define a dictionary

>>> D1 = {//key-value format

"Student1": "Xiaoming",

"Student2": "Zhangsan",

"Student3": "Lisi"

}

2. View

>>> print (d1["student1"])//used with the known key, error when this key is not available

Xiaoming

>>> Print (D1.get ("Student2"))//get method does not error

Zhangsan

>>> Print (D1.get ("Student1"))

None


3. Change the value of key

>>> print (d1["Student1"])

Xiaoming

>>> d1["student1"] = "xiaoming"//Modify if present, add if not present

>>> print (d1["Student1"])

Xiaoming


4. Delete

>>> del d1["Student1"]

>>> Print (D1)

{' Student2 ': ' Zhangsan ', ' student3 ': ' Lisi '}


5. Judge if there is any data in the dictionary

>>> print ("Student1" in D1)

False


6, Multi-level dictionary nesting

>>> D2 = {

"Weibo": {

"Guomao": ["Wilson", "David", "Lucia"],

"Bagou": ["Zhangsan", "Wangwu", "Lisi"]},

"Xindongfang": {

"Yuquan": ["Piter", "Alen", "CC"],

"Wukesong": ["Shenyang", "Lidong", "Wangwei"]}

}


7. Modify the values in the nested menu

>>> print (d2["Weibo" ["Guomao"])

[' Wilson ', ' David ', ' Lucia ']//before modification

>>> d2["Weibo" ["Guomao"][1] = "David"

>>> print (d2["Weibo" ["Guomao"])

[' Wilson ', ' David ', ' Lucia ']//after modification


8. Print all values in the dictionary

>>> print (D2.values ())


9. Print all keys in the dictionary

>>> print (D2.keys ())


10, if the dictionary exists this key is returned, does not exist to create

>>> Print (D2)

{' Weibo ': {' Guomao ': [' Wilson ', ' David ', ' Lucia '], ' Bagou ': [' Zhangsan ', ' Wangwu ', ' Lisi ']}, ' Xindongfang ': {' Yuquan ': [' Pit Er ', ' Alen ', ' cc '], ' wukesong ': [' Shenyang ', ' Lidong ', ' Wangwei '}}

>>> D2.setdefault ("Weibo", {"Xinhua": ["AA", "BB", "CC"]})

{' Guomao ': [' Wilson ', ' David ', ' Lucia '], ' Bagou ': [' Zhangsan ', ' Wangwu ', ' Lisi ']}

>>> D2.setdefault ("Yingfu", {"Xinhua": ["AA", "BB", "CC"]})

{' Xinhua ': [' AA ', ' BB ', ' CC ']}

>>> Print (D2)

{' Weibo ': {' Guomao ': [' Wilson ', ' David ', ' Lucia '], ' Bagou ': [' Zhangsan ', ' Wangwu ', ' Lisi ']}, ' Xindongfang ': {' Yuquan ': [' Pit Er ', ' Alen ', ' cc '], ' wukesong ': [' Shenyang ', ' Lidong ', ' Wangwei ']}, ' Yingfu ': {' Xinhua ': [' AA ', ' BB ', ' CC '}}


11, merge two dictionaries, if the dictionary has the same key is updated, no add

>>> D1

{' Student2 ': ' Zhangsan ', ' student3 ': ' Lisi '}

>>> D3 = {"Student2": "AAAA", "bbbbb": "CCCCC", 1:2}

>>> D1.update (D3)

>>> Print (D1)

{' Student2 ': ' AAAA ', ' student3 ': ' Lisi ', ' bbbbb ': ' CCCCC ', 1:2}


12. Convert Dictionaries to lists

>>> print (D1.items ())

Dict_items ([' Student2 ', ' AAAA '), (' Student3 ', ' Lisi '), (' bbbbb ', ' CCCCC '), (1, 2)])

Can cycle print

>>> for K,v in D1.items ():

.. print (K,V)

...

Student2 AAAA

Student3 Lisi

BBBBB CCCCC

1 2



13, the Dictionary of the Cycle

>>> for I in D1:

... print (i)//only key

...

Student2

Student3

bbbbb

1


>>> for I in D1:

... print (i,d1[i])//Pass I as key into the dictionary query

...

Student2 AAAA

Student3 Lisi

BBBBB CCCCC

0 S


14, for K,v in D1.items (): And for I in D1:print (I,d1[i]) difference

The second way is more efficient, because the first way to convert the dictionary to a list, in the case of large amounts of data, will lead to waste of resources.

Python dictionary operations

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.