Python's elegant Operation dictionary "turn"

Source: Internet
Author: User

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

1.1 Creating a dictionary

There are two ways python can create dictionaries, the first is using curly braces, and the other is using the built-in function dict

{}dict() 
1.2 Initializing Dictionaries

Python can initialize a dictionary when creating a dictionary

{' cold '}dict(' cold '# more elegant     

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

' Name ':' cold '# {' Name ': ' Cold '}dict(' cold '# {' key ': ' Cold '} 

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

The Python dictionary is also initialized by using the Fromkeys method of the dictionary to get the element as a key from the list and class 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 Elegant Get key value

The dictionary can get the value of the key in this way

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

But if you get a Keyerror exception that is triggered by the value of a nonexistent key, the dictionary has a get method that can be used to get the dictionary more gracefully using the dictionary get method.

>>>Info=dict (name=  ' cold ' blog= ' www.linuxzen.com '  >>> info. Get ( ' name ' )  ' cold ' >>> span class= "n" >info. Get ( ' blogname ' ) none>> > info. Get ( ' blogname '  ' Linuxzen '  ' Linuxzen '               

We see that using the Get method to get a nonexistent key value does not trigger an exception, and the Get method receives two parameters and returns the value of the second parameter when the key is not present. We can see that using get is more elegant

1.4 Update/Add

Python dictionaries can use keys as indexes to access/update/Add values

>>>Info=Dict()>>>info[ ' name '  =  Cold ' >>> info[ ' blog '  Span class= "o" >=  ' linuxzen.com ' >>> info{  ' blog ' :  ' linuxzen.com '   ' name ' Span class= "P" >:  ' cold ' }>>> info { ' blog ' :  ' linuxzen.com '   ' name ' :  ' cold Night ' }       

The Update method of the Python dictionary can also be updated and added to the dictionary

>>>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 Span class= "o" >>>> info{ ' blog ' :  ' www.linuxzen.com '  ' name ' :  Cold '  ' blogname ' :  ' Linuxzen ' }   

The Update method of the Python dictionary can be used to update the dictionary using a dictionary, or you can update a dictionary in the same way that parameters pass like a dict function, while the second of the functions in the above code is more elegant, but similarly to the Dict function, where the key is a variable and only the literal value is used.

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 ' ) Span class= "o" >>>> info{ ' blog ' :  ' linuxzen.com '   ' name ' :  ' cold ' Span class= "p" >}>>> del info[  ' name ' ]>>> info{  ' blog ' :  ' linuxzen.com ' }      

You can also use the Pop method of the dictionary to remove a key value and delete

Dict(name= 'cold 'Blog=' linuxzen.com ')info.  Pop(' name ')' cold 'info{' blog 'linuxzen.com '}   
1.6 Other operations

Get all Keys

Dict(name= 'cold 'Blog=' linuxzen.com ')info.  Keys()[' blog 'name ']          

Get Key,value and loop

>>>Info= dict (name= " Cold ' blog= ' linuxzen.com ' ) Span class= "o" >>>> for keyvalue in info. Items (): print key ': '  Valueblog : linuxzen. Comname : cold         

The above information is from: https://www.linuxzen.com/python-you-ya-de-cao-zuo-zi-dian.html

Python's elegant Operation dictionary "turn"

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.