The way to organize Python's most basic operations dictionary _python

Source: Internet
Author: User

The dictionary in Python is the data structure of a key-value mapping in Python, and here's how to manipulate the dictionary gracefully.
1.1 Creating a dictionary

Python has two ways to create dictionaries, the first of which is to use curly braces, and the other is to use the built-in functions Dict

>>> info = {}
>>> info = dict ()

1.2 Initialization Dictionary

Python can initialize a dictionary when it is created

>>> info = {"Name": ' Cold '}
>>> info = dict (name = ' cold ')    # more Elegant

It is obvious that the second method is more elegant and reduces the input of some special characters, but there is a situation where the second type is not competent

>>> key = ' name '
>>> info = {key: ' cold '} # {' Name ': ' Cold '}
>>> info = dict (key = ' C Old ') # {' Key ': ' Cold '}

Obviously the second method raises a bug that is not easy to find

The Python dictionary also has an initialization way of using the Fromkeys method of the dictionary to get the element as a key from the list and initialize it with the second argument of the None or Fromkeys method.

>>> info = {}.fromkeys ([' Name ', ' blog '])
>>> info
{' blog ': None, ' Name ': none}
>> > info = dict (). Fromkeys ([' Name ', ' blog '])
>>> info
{' blog ': None, ' Name ': none}
>>> info = Dict (). Fromkeys ([' Name ', ' blog '], ' linuxzen.com ')
>>> info
{' blog ': ' linuxzen.com ', ' name ': ' Linuxzen.com '}

1.3 Gracefully get the key value

The dictionary can thus get the value of the key

>>> info = {' name ': ' Cold ', ' blog ': ' linuxzen.com '}
>>> info[' name ']
' cold '

But if you get a Keyerror exception that triggers the value of a nonexistent key, the dictionary has a get method that you can use to obtain a dictionary in a more elegant way.

>>> info = dict (name= ' cold ', blog= ' www.linuxzen.com ')
>>> info.get (' name ')
' cold '
>>> info.get (' blogname ')
None
>>> info.get (' blogname ', ' Linuxzen ')
' Linuxzen '

We see that using the Get method does not trigger an exception when the key value is not present, and the Getting method receives two parameters and returns the value of the second parameter when the key is not present we can see the elegance
1.4 Update/Add

The Python dictionary can use keys as an index to access/update/Add values

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

Also, the Python dictionary's Update method can update and add dictionaries

>>> info = dict (name= ' cold ', blog= ' linuxzen.com ')
>>> info.update ({' Name ': ' Cold Night ', ' Blogname ': ' Linuxzen '})
>>> info
{' blog ': ' linuxzen.com ', ' name ': ' Cold Night ', ' blogname ': ' Linuxzen '}
>>> info.update (name= ' cold ', blog= ' www.linuxzen.com ') # more elegant
>>> info
{' Blog ': ' www.linuxzen.com ', ' name ': ' Cold ', ' blogname ': ' Linuxzen '}

The Python dictionary Update method can use a dictionary to update the dictionary, or you can update a dictionary in the same way as the Dict function by passing parameters like the one in the code, but the second is more elegant, but similar to the Dict function, when a key is a variable, it only takes a literal value.
1.5 Dictionary deletion

You can call the Python built-in keyword del to delete a key value

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

You can also use the dictionary pop method to take out a key value and delete

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

1.6 Other operations

Get all key

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

Get Key,value and loop

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

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.