The concept and basic operation of the python dictionary

Dictionaries are very commonly used as a data structure that json data format is very similar to the core data is stored in key-value pairs, in Python dictionary on four points described as follows:

1. The configuration of the dictionary is represented by a subject in need braces {}, each dictionary element is present in the form of key-value pairs, and between the key state of the English '': '' spaced

2. The key is unique in the dictionary, can not be duplicated, the need for key character enclosed in quotation marks. Value may be a single value, multiple values ​​may be composed of a list of tuples or dictionaries

3. The dictionary is no longer the sequence, can not pass up the element index value of the acquisition is completed, can only be achieved through the key index.

4. dictionaries and lists, are variable types of data structures.

dict1 = { 'Name': 'John Doe', 'age': '33 ',' gender ':' male ',' children ': {' son ':' ZHANG Si ',' Daughter ':' Madeleine ' }, 'interest': [ 'play', 'swim', 'singing']}

print(dict1)

print (dict1 [ 'aged'])

print (dict1 [ 'children'] [ 'son'])

print (dict1 [ 'interest'] [1])   

out:

{ 'Name': 'John Doe', 'age': '33', 'children': { 'son': 'ZHANG Si', 'Daughter': 'Madeleine'}, 'sex': 'male' 'interest': [ 'play', 'swim', 'singing']}

33 

ZHANG Si
Swimming 

# For the dictionary, it is no longer the sequence, the result output by the first shows, when the structure element dictionary and dictionary elements order in which output has changed, you want to get the value of the element, only
the index writing specific keys inside.


The basic method of operation of the dictionary elements:
increase of dictionary elements 1
is generally achieved using the following methods: namely: setdefault, update method and a key index method:
take the above example:
dict1.setdefault ( 'residence' , 'Hefei') # dictionary to increase household registration information to note here that is not ( 'household' : 'Hefei')
dict1.update ({ 'education': 'Master'}) # add information to increase education by way of update, adding that a dictionary object
dict1 [ 'height'] = 178 # increase height information directly target set by the direct method and assign key index to

2. delete the dictionary elements
on the element can be used to delete the dictionary pop, poptiem and clear three kinds methods.
For example:
dict1.pop ( 'household') # delete the household registration information
dict1 [ 'children'] .pop ( 'daughter') # delete the dictionary daughter's name
dict1.popitem () # delete the last element dictionary
dict1.clear ( ) Clear # dictionary elements

3. modify the dictionary elements
dictionary elements modifications mentioned as increasing portion, and the update methods may also be used to complete the modification method of the dictionary search key element. Note that if the value of the dictionary in the dictionary is another or list,
Key index achieved by the need to query the dictionary element corresponding to the application and the modification method can be based on the query (such as update method, instead of) lists several additional important method is as follows:.
Dict2 {= 'movie' : [ '3 Idiots',' Odyssey Taishengquqin ',' crazy city animal '],
' director ': [' Joe Smith ',' John Doe ',' Andy ']' rating ': [9.1,9.2,9.3]}
Print (dict2.get (' score '))
Print (dict2.keys ())
Print (dict2.values ())
Print (dict2.items ())
function and get method indexing is known, a value corresponding to the key can be removed from the dictionary except that, if a key does not exist in the dictionary, methods of indexing the generated 'key error' information;.
and error method does not get If not, then will get to display None, will not affect the normal execution .keys other scripts, values and methods were items taken all the keys in the dictionary, and key value pairs.


Guess you like

Origin www.cnblogs.com/tinglele527/p/11684228.html