Python dictionary operations

Source: Internet
Author: User

A key-value data type, used as a dictionary of our school, to check the details of the corresponding page by strokes and letters.

Grammar:

#!/usr/bin/env python#-*-Coding:utf-8-*-info = {' stu1101 ': ' Tenglan Wu ', ' stu1102 ': ' Longze luola ', ' stu110 3 ': "Xiaoze Maliya",}print (info) execution result: {' stu1103 ': ' Xiaoze maliya ', ' stu1102 ': ' Longze luola ', ' stu1101 ': ' Tenglan Wu ' }

Features of the dictionary:

    • Dict is disordered.

    • Key must be unique and so is inherently heavy

Increase

>>> info["stu1104"] = "Cang jing Empty" >>> info{' stu1102 ': ' Longze luola ', ' stu1104 ': ' Cang jing empty ', ' stu1103 ': ' Xiaoze Mal Iya ', ' stu1101 ': ' Tenglan Wu '}

Modify

>>> info[' stu1101 ' = "Enrique" >>> info{' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya ', ' stu1101 ': ' Enrique '}

Delete

>>> info{' stu1102 ':  ' Longze luola ',  ' stu1103 ':  ' Xiaoze maliya ',  ' stu1101 ':  ' Enrique '}>>> info.pop ("stu1101")  # Standard removal posture ' enrique ' >>> info{' stu1102 ':  ' Longze luola ',  ' stu1103 ':  ' xiaoze  Maliya '}>>> del info[' stu1103 ']  #换个姿势删除 >>> info{' stu1102 ':  ' LongZe  luola '}>>> >>> >>> >>> info = {' stu1102 ':  ' Longze luola ',  ' stu1103 ":  ' Xiaoze maliya '}>>> info{' stu1102 ' :  ' Longze luola ',  ' stu1103 ':  ' Xiaoze maliya '}  #随机删除 >>>  Info.popitem () (' stu1102 ',  ' Longze luola ') >>> info{' stu1103 ':  ' Xiaoze maliya ' }


Find

>>> info = {' stu1102 ': ' Longze luola ', ' stu1103 ': ' Xiaoze maliya '}>>> >>> ' stu1102 ' in info #标准 Usage true>>> info.get ("stu1102") #获取 ' Longze luola ' >>> info["stu1102"] #同上, but look below ' Longze Luola ' >>  > info["stu1105"] #如果一个key不存在, error, get no, not exist only return Nonetraceback (most recent call last): File "<stdin>", line 1, in <module>keyerror: ' stu1105 '

multilevel dictionary nesting and manipulation

av_catalog = {     "Europe and America":{          "www.youporn.com":  ["a lot of free, the world's largest", "Quality General"],          "www.pornhub.com":  ["a lot of free, also very big", "quality than Yourporn High"],          "letmedothistoyou.com":  ["Many are selfies, high-quality pictures many", "resources are not many, update slow"],          "x-art.com": ["high quality, really high", "all charges, Dick than please bypass"]    },     "Japan and Korea" :{         "Tokyo-hot": ["How the quality is not clear, the individual has not liked the Japanese and Korean fan", "Heard is the fee"]     },     "mainland":{         "1024": ["All free, really good, A good man's life is safe "," the server in foreign countries, slow "]    }}av_catalog[" "["] "][1] += ", you can crawl down with the crawler "Print (av_ catalog["mainland" ["1024x768"]) #ouput  [' All free, really good, a good person life safe ',  ' server is abroad, slow, can crawl down with crawlers '] 

other poses

#values >>> info.values () dict_values ([' Longze luola ',  ' Xiaoze maliya ']) #keys > >> info.keys () Dict_keys ([' stu1102 ',  ' stu1103 ']) #setdefault >>> info.setdefault (" Stu1106 "," Alex ") ' Alex ' >>> info{' stu1102 ':  ' Longze luola ',  ' stu1103 ':  ' Xiaoze  maliya ',  ' stu1106 ':  ' Alex '}>>> info.setdefault ("stu1102", "deflorating la") ' longze  Luola ' >>> info{' stu1102 ':  ' Longze luola ',  ' stu1103 ',:  ' Xiaoze maliya ',   ' stu1106 ':  ' Alex '} #update  >>> info{' stu1102 ':  ' Longze luola ',  ' stu1103 ':  ' Xiaoze maliya ',  ' stu1106 ':  ' Alex '}>>> b = {1:2,3:4,   "stu1102": "Deflorating"}>>> info.update (b) >>> info{' stu1102 ':  ' deflorating ',  1:  2, 3: 4,  ' stu1103 ':  ' Xiaoze maliya ',  ' stu1106 ':  ' Alex '}# Itemsinfo.items () Dict_items ([(' Stu1102',  ' deflorating '),  (1, 2),  (3, 4),  (' stu1103 ',  ' Xiaoze maliya '),  (' stu1106 ',  ' Alex ')] #通过一个列表生成默认dict, there is no way to explain the pit, less use this >>> dict.fromkeys ([three-way], ' testd ') {1:   ' testd ', 2:  ' testd ', 3:  ' testd '}

Cyclic dict

#方法1for key in Info:print (Key,info[key]) #方法2for k,v in Info.items (): #会先把dict转成list, do not use print (K,V) when the data is large





This article is from the "Little Rookie" blog, please be sure to keep this source http://baishuchao.blog.51cto.com/12918589/1935077

Python dictionary operations

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.