Python Basics (v)

Source: Internet
Author: User

1. Dictionaries

Dict using {} to represent key value pair data {Key:value} Uniqueness

Key must be a hash immutable data type that can be used as a key in the dictionary

there is no limit to the value

Dict saved data is not saved in the order that we added it. It is stored in the order of the hash table. And the hash table is not sequential. Therefore, the slicing work is not possible.
It can only get the data in Dict by key.
  
    Note: Key must be immutable (hashed). value is not required. Can save any type of data
2. Adding and deleting changes
  
1) Increase

Dic[key] = value

Dic.setdefault (Key,value) if the key exists in the dictionary without any action, add

Can be queried by key, without this key to return none
  

    ====================================================================
2) Delete

Pop (key) has a return value that is returned by the deleted value
Del Dic[key]
Popitem () random deletion
Clear () empty Dictionary
  

    ======================================================================
3) Modify

Dic[key] = value
Dic.update (dictionary)
  

    =======================================================================
4) Query

Get (Key)
Dic[key]
For loop
SetDefault (Key)


=======================================================================

3. Other operations of the dictionary:

Keys gets to all the keys that exist in a high-imitated list

Values gets to all the values that exist in a high-imitated list

Items gets to all the key values in the form of a meta-ancestor exist in a highly imitated list

Deconstruction:

A, B = 1, 2

A , B = (up to)

b = [+]

4. Nesting of dictionaries:

DIC = {
' Name ': ' Wang Feng ',
' Age ': 43,
' Wife ': {
' Name ': ' International Chapter ',
' Age ': 39,
' Salary ': 100000
},
' Baby ': [
{' name ': ' Big Bear ', ' age ': 18},
{' name ': ' Bear II ', ' age ': 15},
]

}

dic[' baby '][0][' age ' = 19

Print (DIC)

Python Basics (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.