python dictionary of related operations summary

Look at the recent python machine learning-related code, I saw a lot of operations on a dictionary, his own impression is not so deep, and here he is finishing a refresher, to share with you out, if wrong place, please leave a message after seeing We will revise, thanks!

 

The Python Python dictionary is a map of key value data structure, related as follows:

1.1 create a dictionary

There are two ways to create a Python dictionary, first is to use curly braces, and the other is to use the built-in function dict

. 1 dictionary1} = {    # method 
2 dicttionary1 = dict ()    # Method II

1.2 pairs dictionary initialize

Python dictionary can be initialized when creating the dictionary, the definition of variables like after its initialization is directly analogous, collective operation is as follows

1 dictionary1 = {“name”:'wind_under_the_wing'}      #方法一
2 dictionary2 = dinct(name='wind_under_the_wing')    #方法二

In fact, the second method is apt to cause problems in drying out the code here we are interested to analyze their own

1 key = 'name'
2 dictionary1 = {key:'wind_under_the_wing'}     #{'name':"wind_under_the_wing'}
3 dictionary2 = dict(key='wind_under_the_wing')   #{'key':'wind_under_the_wing'}

Use fromkeys () method to get the item from the list as a key

>>> info = {}.fromkeys(['name', 'blog'])
>>> info
{'blog': None, 'name': None}
>>> info = dict().fromkeys(['name', 'blog'])
>>> info
{'blog': None, 'name': None}
>>> info = dict().fromkeys(['name', 'blog'], 'wind-under-the-wing')
>>> info
{'blog': 'wind-under-the-wing', 'name': 'wind-under-the-wing'}

1.3 acquiring key

>>> info = {'name':'linux', 'blog':'wind-under-the-wing'}
>>> info['name']
'linux'

But if there is no key to get the value will trigger a KeyError abnormal, get a dictionary method, you can use the dictionary get more elegant method of obtaining dictionary

>>> info = dict(name= 'linux', blog='wind-under-the-wing')
>>> info.get('name')
'linux'
>>> info.get('blogname')
None
>>> info.get('blogname', 'linux')
'linux'

We see using the get method to get the key does not exist will not trigger an exception when, at the same time get method accepts two parameters, when the key does not exist will return the value of the second argument, we can see that get more use elegance

 

1.4 Delete dictionary

You can call Python built-in keywords del to delete a key

>>> info = dict(name='cold', blog='linuxzen.com')
>>> info
{'blog': 'linuxzen.com', 'name': 'cold'}
>>> del info['name']
>>> info
{'blog': 'linuxzen.com'}

 

But also you can use the dictionary pop method to remove a key, and delete

>>> info = dict(name='cold', blog='linuxzen.com')
>>> info.pop('name')
'cold'
>>> info
{'blog': 'linuxzen.com'}

1.5 Other operations

Get all key

>>> info = dict(name='cold', blog='linuxzen.com')
>>> info.keys()
['blog', 'name']

 

Get key, value and recycling

>>> info = dict(name='cold', blog='linuxzen.com')
>>> for key, value in info.items():
...     print key, ':',  value
...
blog : linuxzen.com
name : cold

 

 

Guess you like

Origin www.cnblogs.com/wind-under-the-wing/p/11769244.html