Python Full stack __ dictionary and its operation

Source: Internet
Author: User
Tags key loop

1. Dictionaries

Dictionary, one of the underlying data types, {} stores data as a key-value pair.

Store data in key:value form. For example, name is Key,laonanhai as value.

DIC = {' name ': ' Laonanhai ',        ' age ': ' name_list ':       [' negative ', ' junny ', ...]       }

The only type of mapping in Python. Used to store large amounts of relational data.

Classification of data types:

Variable class:

Variable (non-hashed hash): list, Dict, set. For example list = [11, 222, 333]

Immutable (hashed hash): STR, int., tuple, BOOL. For example name = ' Alex '

Container class:

The data type of the container class: list, dict, tuple. For example L1 = [One, True, ' Alex ', [22, 33, 44]]

Data types for non-container classes: int, str, bool. For example S1 = ' [11, 22, 33] '

The key of the dictionary must be an immutable data type and is unique.

Hash algorithm:

The key of the dictionary is converted to a number by a hash table, and a binary search is used to find the number.

The value of the dictionary can make arbitrary data types.

Dictionary queries are very fast and can store large amounts of relational data.

The dictionary is unordered before Python 3.5 contains 3.5, but is ordered after 3.6.

2, the dictionary additions and deletions and other operations

The key is unique when the same key is present in the back of the overlay front. For example:

DIC = {        ' name ': ' Alex ',        ' name ': ' Wusir '}print (DIC)

The key must be a data type that is immutable and can be hashed. When the key is mutable and the non-hashed data type is, the output indicates an error. For example:

DIC = {        ' name ': ' Alex ',        [1, 2, 3]: ' Value '}print (DIC)

2, 1 increase 1, __[' a '] = ' B ' A is the key, B is the value. Add directly, then overwrite, none.

None is added.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic[' sex '] = ' laddy_boy ' Print (DIC)

There is the overwrite.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic[' name '] = ' Alex ' Print (DIC)

2, __.setdefault (' A ', ' B ') A is the key and B is the value. There is no change in responsibility, nothing is added.

None is added.

When you add only a, the corresponding B value defaults to none.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic.setdefault (' sex ') print (DIC)

None is added.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic.setdefault (' sex ', ' Laddyboy ') print (DIC)

The responsibility is the same.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic.setdefault (' name ', ' Alex ') print (DIC)

2, 2 delete 1, __.pop (' A ') has a return value, the value returned is the key corresponding.
DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}print (Dic.pop (' ages '))
Print (DIC)

When this key is not available, the default return value can be modified.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}print (' age1 ', ' no this key ... ') print (DIC)

2, __.clear () empty the dictionary.
DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic.clear () print (DIC)

3, __.popitem () random deletion, there is a return value, the return value is a tuple, the inside is the deletion of key value pairs.

Before Python 3.5 and containing 3.5 are unordered deletions, Python 3.6 is the last one to delete the dictionary after.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}print (Dic.popitem ()) print (DIC)

4, Del

(1) Del __ deletes the entire dictionary.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}del dicprint (DIC)

(2) Del dic[' a '] deletes the corresponding key-value pair according to key A.

DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}del dic[' name ']print (DIC)

2, 3 change 1, __[' a '] = ' B ' has the overwrite, no is added.
DIC = {' name ': ' Old boy ', ' age ': ' Hobby ': ' Women '}dic[' name '] = ' Alex ' Print (DIC)

2, A.update (B) AB is two dictionaries respectively.  Update of two dictionaries. The content overlay of the B dictionary is added to a, and B is unchanged.
DIC = {"Name": "Jin", "Age": +, "sex": "male"}dic2 = {"Name": "Alex", "Weight": 75}dic2.update (DIC) print (DIC) print (DIC2)

2, 4 Check 1, __[' A "] A is the key.
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (dic[' name '])

When a key is not found in the dictionary, print an error.

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (dic[' name1 '))

2, __.get (' a ') A is the key.
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (dic.get (' name '))

When the key dictionary is not found, the default printout is none. For example:

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (dic.get (' name1 '))

When the key dictionary is not found, the default output can be modified to the content you want. For example:

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (dic.get (' name1 ', ' SB, no This key '))

3, for loop output key.
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}for i in dic:        print (i)

4, __.keys () the content of the printout is the key, the printout format is Dict_keys
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (Dic.keys (), type (Dic.keys ()))

5, __.values () the content of the printout is a value, the printout format is dict_values.
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (Dic.values (), type (Dic.values ()))

6, __.items () the content of the printout is a key-value pair, placed in the tuple. Print output in dict_items format
DIC = {"Name": "Jin", "Age": +, "sex": "Male"}print (Dic.items (), type (Dic.items ()))

Use the For loop to print the key loop.

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}for key in Dic.keys ():        print (key)

Convert the key to a list to print out.

DIC = {"Name": "Jin", "Age": +, "sex": "male"}l_key = List (Dic.keys ()) print (L_key)

Use the For loop to print the values in a loop.

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}for I in Dic.values ():        print (i)

Convert the output to a list to print out.

DIC = {"Name": "Jin", "Age": +, "sex": "male"}l_values = List (Dic.values ()) print (l_values)

7. The concept of assigning values separately
A, B = 1, 2print (A, B)

A, B = (' Alex ', ' (') ') print (A, B)

A, B = [' Alex ', ']print ' (A/b)

There are the following:

A = 1

b = 3

A B-value interchange is implemented with one line of code.

A = 1b = 3a, B = B, Aprint (A, B)

For i in [+,]:        print (i)

For i in [(One, All), (+, a), (in.)]:        print (i)

K, v = (' name ', ' old boy ') print (k, v)

DIC = {"Name": "Jin", "Age": +, "sex": "Male"}for K, V in Dic.items ():        print (k, v)

3, the Dictionary of Nesting
DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' age ':        ' hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}

1. Add a key value pair to DIC, school: Old boy.

DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' age ' : +,        ' hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}dic[' school '] = ' old boy ' print (DIC)

2. Change the value of status to true.

DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' Age ': '        hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}dic[' status ' = Trueprint (DIC)

3. Add an element of ' flat ' to name_list.

DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' age ':        ' hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}dic[' name_list '].append (' flat ') print (DIC)

4. Add a key value to the dictionary that corresponds to personal_msg sex: male.

DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' age ' :        ' hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}dic[' personal_msg ' [' sex '] = ' male ' Print (DIC)

5. Change the perm in the hobby_list corresponding list to sing.

DIC = {    ' name_list ': [' Bowl Rong ', ' junny ', ' cool sister '],    ' status ': None,    ' personal_msg ': {        ' name ': ' Negative ',        ' age ':        ' hobby_list ': [' smoking ', ' drinking ', ' perm '],    }}dic[' personal_msg ' [' hobby_list '][-1] = ' singing ' Print (DIC)

Python Full stack __ dictionary and its operation

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.