Python Learning data Type-Dictionary Dict

Source: Internet
Author: User

Dictionary: Dict

1. The dictionary is the only type of mapping in Python and consists of value pairs enclosed in {};

2, the dictionary key is unique, at the time of saving, according to key to calculate a memory address, how to put value in this memory, this is called the hash algorithm;

3. The key in the dictionary must be hashed (immutable data type Str,int,tuple,bool)

4. Value in the dictionary can be stored in any data type

5, the dictionary is also disorderly, it is not indexed to find the value of a said, can only be found by key value

Grammar:

Dict = {key:value, ...}

Increase:

1. Add directly

DIC ["Beijing"] = "political Center"

Note: If the dictionary does not have the "Beijing" key, then add, if any, change the corresponding value value to the new

2, SetDefault (Key,value) method added, value is not write, default is None

DiC. SetDefault ("China")---> {' China ': None}

DiC. SetDefault ("China", "Chinese")--->{"country": None}, once the key is added through the SetDefault method, then the value corresponding to the change key cannot be changed.

By deleting:

1, specify the key value to delete

ret = Dict.pop (key) has a return value, which returns the deleted value, like List, str and tuple are immutable data types so no pop method

Del dict Deleting a Dictionary object

Del Dict[key] Press key to delete, no return value, none

2, randomly delete dict.popitem ()

ret = Dict.popitem () #随机删除字典中的元素, returns a tuple (Key,value) containing a key-value pair

Deconstruction:

K,v = Dict.popitem ()

Print (K,V)

3. Empty the Dictionary

Dict.clear ()

Change:

1. Give new value directly via key

Dict[key] = New_value

2. Two dictionaries merged into one dict_to.update (Dict_from)

Dict3 = {"Name": "Sun", "Age": 30}
Dict4 = {"Name": "Xiu", "age": +, "hobby": "Play_tv"}
Dict3.update (DICT4) # Updates the Dict4 to DICT3, if the key exists in Dict3, the corresponding value value will be overwritten and if not present, the new

Check:

1. Check value by key

Dict[key] If key does not exist, the error

Dict.get (key, custom return value) such as key does not exist, default returns None, can also be customized

2. Query the dictionary for all Keys dict_keys ([' Name ', ' age ', ' hobby ')

For I in Dict.keys:

Print (i)

3. Query all values in the dictionary dict_values ([' Xiu ', ', ' Play_tv '])

For I in Dict.values:

Print (i)

4. Query all the elements in the dictionary items dict_items ([' Name ', ' Xiu '), (' Age ', ' + '), (' Hobby ', ' PLAY_TV ')])

For I in Dict.items:

Print (i) #i is a tuple

Python Learning data Type-Dictionary Dict

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.