"Turn" Python Learning (9) dictionary

Source: Internet
Author: User

Transfer from http://www.cnblogs.com/BeginMan/p/3156960.html

First, the mapping type

I understand that the mapping type in is: The relationship of key-value pairs, key (key) mapping values (value), and they are one-to-many relationships.
The dictionary is the only type of mapping for Python.

Extension 1: Hash table
A data structure in which the values are stored according to the relevant key, forming a "key-value pair" (Key-value pairs), and the values in the hash table are not sequential.

Extension 2: Difference between a mapping type and a sequence type

1): Different access modes, sequence types are keyed with numeric types, and map types can be keyed with other object types (generic string)

>>> lis = [‘A‘,‘B‘,‘C‘]>>> lis[2]‘C' >>> dic = {‘name ": " a",  '  Father ":  ' b",  ' mother  ": " c ' 

2): Different types of storage, the key of the mapping type, directly or indirectly related to the value.
3): Sequence type, ordered column, map type unordered column

Second, the dictionary

1, Factory Method Dict ():

>>> tu= ([‘A‘,‘B‘],[‘Xx‘,‘Yy‘]) >>>run[‘A ", " b '], [ ' xx ",  ' yy" ) >>> fdict = Dict (TU) >>> Fdict{ ' a ":  "b", yy"}          

2. Access form:

>>>dic{‘Father‘:‘B‘,‘Name‘:‘A‘,‘Mother‘:‘C‘}>>>For objInchDic:PrintObjfathernamemother>>>For objInchDic.Keys ():PrintObj fathernamemother>>>For objInchDic.VALUES ():PrintObj bac>>>For objInch DIC. items (): print obj ( ' father ",  "b"  ) ( ' name ",  ' a"  ' mother ",  ' c ) >> >                 

3, Has_key (), in, not in to check if there is a key, Has_key () has been slowly discarded.

4, there is update, no add

5. Delete

Del dic['name']   # Delete element # Delete entire      

6. Operator
[], and in, not in

'nameinprint dic['name']a      

7. Correlation function
Dict (): Creating a Dictionary
Len (): Returns the number of key-value pairs
Hash (obj): Returns the hash value of obj

8. Built-in method
Dict.clear (): Delete all elements in the dictionary
Dict.copy (): Shallow copy
Dict.formkeys (): Creating a Dictionary
Dict.get (Key,default=none): Returns the corresponding key value
Dict.has_key (): Key is present
Dict.items (): Returns a list of key-value pairs Ganso in the dictionary
Dict.keys (): List of Keys dict.values (): List of values
......

>>>Dic.keys () [‘Father‘, "name",  ' mother"  Dic.values () [ " b ",  '  A ",  ' c ' ]>>> dic.get (" name ")  ' a"       

Third, attention

1, does not allow a key to correspond to multiple values, a key can only correspond to one item
2, when the key conflicts, take the last one.
3. Python does not check for key conflicts, nor does it cause errors due to conflicting keys, and it is bound to consume a lot of memory if each key is checked for conflicts.

 >>> dic={ ' a  ": " ss ', a": " xxxx}> >> Dic{ "a ": " xxxx  "            

4, the key must be hashed.
All immutable types are hashed, and immutable types such as (lists, dictionaries) are not.
Immutable types: String,integer,tuple,
Variable type: list,dict

5 . Numbers with the same value represent the same keys, such as the hashes of 1 and 1.0 are the same, so they are the same key.

>>> dic={1:'a', 1.0:'b'}>>>'b'}  

"Turn" Python Learning (9) dictionary

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.