Python Learning (v)

Source: Internet
Author: User

Dictionary Dict

Set the dictionary to

DIC = {"name":"andraw"}

A, increase

1, increase the key value pair, increase the key value to Address:guangdong in the dictionary dic

dic["address""Guangdong"

2, increase by SetDefault method, add key value to Address:guangdong in dictionary dic

Dic.setdefault ("address","Guangdong")

3. Difference between the two

Method One, if the dictionary already exists the key value, then directly overwrite, there is no key value, then establish; method two, if the dictionary already exists key value, do not operate, if not exist, then establish

B, delete

1, delete by pop method, delete address key value pair in dictionary, return corresponding values

Dic.pop ("address")

Note: If not present, error. Here's how to fix it:

Dic.pop ("address", None)

Note: If the key value does not exist, return none, you can set the return value

2. Delete Via del

del dic["address"]

3, delete by Popitem, random Delete, return deleted Ganso, if empty error

Dic.popitem ()

4. Clear the Dictionary by clear method

Dic.clear ()
C, find

1, through the key value lookup, no error

value=dic["name"]  print(value)

2, by the Get method to find, return values, not return none, can set the return value

Dic.get ("name")
D. Change

1. Direct change via key value pair

dic["name"]="andraw"

2, through the Update method changes, if DIC does not exist Dic2 key value pair, then dic increases the key value of Dic2, if present, the key value corresponding to the values of the value replacement

Dic1 = {"name":"aaa"} dic.update (DIC)

Python Learning (v)

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.