python dict Dictionary Detailed

And the same list, the data dictionary is a collection of many, it belongs to the type of variable sequences. The difference is that it is a variable sequence disorder, its contents are saved "key on the" form of storage.

Dictionary, each element corresponding to the index used to be called keys (Key), each element corresponding to the key value is called (value), the key and its associated value is called "key-value pair."

Common dictionary type like a student Xinhua Dictionary. We know, through the Xinhua Dictionary of syllables, you can quickly find the characters you want to find. Wherein the dictionary syllables is equivalent to a dictionary of keys and characters corresponding to the key value is equivalent.

 

Table 1 Python dictionary feature
Main features Explanation
Rather than reading through the index through the key elements Dictionary type is also sometimes called associative array or hash (hash). It is through a series of key value to link, so you can get the specified entry from the dictionary by key, but you can not get through the index.
A dictionary is an unordered collection of any data type And a list of different tuples, will usually index 0 corresponding elements are referred to the first element. The dictionary elements are unordered.
Dictionary is variable, and may be arbitrarily nested Dictionary can grow in situ, or shortened (without generating a copy), and it supports nested to any depth, i.e., the value stored in the dictionary may be a list or other dictionaries.
The key must be unique in the dictionary Dictionary, does not support the same key repeatedly appear, otherwise, it will only retain the last key-value pair.
The dictionary keys must be immutable Dictionary values ​​are immutable, just numbers, strings or tuples lists can not be used.

Python dictionary type in the equivalent  Java  or  C ++  the Map object.

 

1) braces syntax to create dictionary

Each element in the dictionary contains two parts, namely the key and value, when creating the dictionary, separated by a colon between the key and values, separated by commas between adjacent elements, all the elements in the braces {} .

Python dictionary type syntax is as follows:

key: valuen represent key for each element. It should be noted that the same dictionary key value of each element must be unique.

dictname = { ' key ' : ' value1 ' , ' key2 ' : ' value2 ' , ..., ' keyn ' : surplus}

 

 

[dict the root @ Kube] # CAT demo.py 
# Coding: UTF-. 8 

Scores = { ' language ' : 89, ' mathematics ' : 100, ' chemical ' : 90 }
 Print (type (Scores)) 
a_scores = { ' languages ' : 89, ' mathematics ' : (44, 55), ' chemical ' : [90,66 ]} #key key list is not given
 Print (a_scores) 


[the root @ Kube dict] # Py demo.py 
< class  ' dict ' >
{ ' Language ' : 89, ' mathematics ' : (44, 55), ' chemical ' : [90, 66 ]} 
[@ Kube the root dict] # 

 

 

2) create a dictionary by fromkeys () method

Python, you can also use fromkeys dict dictionary type provided () method creates all the keys to empty dictionary, using this method syntax is:

dictname = dict.fromkeys(list,value=None)
[dict the root @ Kube] # CAT demo1.py 
S_NAME = { ' language ' , ' mathematics ' , ' English ' } 
Scores = dict.fromkeys (S_NAME)
 Print (type (Scores))
 Print (Scores) 
[the root @ Kube dict ] # Py demo1.py 
< class  ' dict ' > 
{ ' Mathematics ' : None, ' English ' : None, ' language ' : None} 
[@ Kube the root dict] #

3) () function to create a dictionary mapping by dict

There are functions to create a dictionary by dict () writing a variety of Table 2 set out the usual ways, they are created with a dictionary a.

Table 2 dict () function to create the dictionary
Creating format Precautions
>>> a = dict(one=1,two=2,three=3) Note that one of the one, two, three are strings, but when you create a dictionary this way, the string can not be quoted.
>>> demo = [('two',2),('one',1),('three',3)] #方式1
>>> demo = [['two',2],['one',1],['three',3]] #方式2
>>> demo = (('two',2),('one',1),('three',3)) #方式3
>>> demo = (['two',2],['one',1],['three',3]) #方式4
>>> a = dict(demo)
To dict () function is passed or a list of tuples, and their elements and each comprising a list of tuples or two elements, wherein the first element as a key element as the second value.
>>> demokeys = [ 'one', 'two', 'three'] # may also be a string or a tuple
>>> demovalues = [1,2,3] # may also be a string or a tuple
>> > a = dict (zip (demokeys , demovalues))
By applying dict () function and the zip () function can be converted to the corresponding first two lists of dictionaries.

Note that regardless of which way to create a dictionary using the above, the key elements of the dictionary can only be a string, tuple or a number, not a list.

 

Access python dictionary

And a list of different tuples, which elements are accessed by subscripts, whereas the different dictionaries, which are accessed through the key element value corresponding to.

Because the elements of the dictionary are unordered, it is not like lists, tuples that, using slices of one-time access to multiple elements.

Python is more recommended get dict type provided by () method to get the value of the specified key. Syntax get () method is as follows:

dict.get(key[,default])
[dict the root @ Kube] # CAT demo1.py 
Scores = { ' language ' : 60, ' mathematics ' : 70, ' English ' : 80 }
 Print (scores.get ( ' language ' ))
 Print (scores.get ( ' English ' )) # get the get function value
 del Scores #del delete dictionary
 Print (Scores) 

[root @ Kube dict] # Py demo1.py 
60 
80 
Traceback (MOST recent Results Last Call): 
  File " demo1.py " , Line 5 ,in <module>
    print(scores)
NameError: name 'scores' is not defined
[root@kube dict]# 

python dict dictionary basic operations

Since the variable sequence part of the dictionary, the dictionary so we can operate in any of the key (key-value pairs). Python  , the common dictionary operations are the following:

    1. Add a new key-value pairs to an existing dictionary.
    2. To modify an existing dictionary key pair.
    3. Removes the specified key-value pairs from the existing dictionary.
    4. It determines whether there is the designated key of an existing dictionary.

 

Python dictionary to add key-value pairs

If you want to add key-value pairs, just as there is no assignment to the key for the dict. To achieve this syntax is as follows: 
dict [Key] = value 

 

Table 1 Python dictionary to add key-value pair syntax parameters
parameter meaning
dict The dictionary name.
key He pledged to add key elements. Note that since it is adding new elements, it is necessary to ensure bond and a dictionary of this element in the existing elements different from each other.
value It represents the value of the data to be added, as long as Python data types supported can be.

 

Guess you like

Origin www.cnblogs.com/zy09/p/11597909.html