Basic operations of the python dictionary and basic operations of python

Source: Internet
Author: User

Basic operations of the python dictionary and basic operations of python

Basic dictionary Methods

What is a dictionary:

A dictionary is a key-value data type. It sounds like a dictionary we use in school. We use strokes and letters to find the details of the drinking page.

Syntax:

id_dict = {    'stu1101': "TengLan Wu",    'stu1102': "LongZe Luola",    'stu1103': "XiaoZe Maliya",}

 

Dictionary features:

Dict is unordered

Key must be unique, value can be repeated, key = key, value = value

 

Added:

id_dict["stu1104"] = "smelond"print(id_dict){'stu1101': 'TengLan Wu', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya', 'stu1104': 'smelond'}

 

Modify:

id_dict["stu1101"] = "amanda"print(id_dict){'stu1101': 'amanda', 'stu1102': 'LongZe Luola', 'stu1103': 'XiaoZe Maliya'}

 

Delete an item:

Print (id_dict)
{'Stu1101': 'tenglan wu', 'stu1102': 'longze Luola ', 'stu1103': 'xiaoze Maliya '}

Id_dict.pop ("stu1101") # standard Deletion
Print (id_dict)
{'Stu1102': 'longze Luola ', 'stu1103': 'xiaoze Maliya '}

 

Del deletion:

Print (id_dict) {'stu1101': 'tenglan wu', 'stu1102': 'longze Luola ', 'stu1103 ': 'aoze Maliya '} del id_dict ["stu1101"] # del Delete print (id_dict) {'stu1102': 'longze Luola', 'stu1103': 'xiaoze Maliya '}

 

Random deletion:

Print (id_dict) {'stu1101': 'tenglan wu', 'stu1102': 'longze Luola ', 'stu1103': 'xiaoze Maliya'} id_dict.popitem () # randomly Delete print (id_dict) {'stu1101': 'tenglan wu', 'stu1102': 'longze Luola '}

 

Delete the dictionary directly:

Print (id_dict) id_dict.clear () # delete all content in the dictionary print (id_dict) {'stu1101': 'tenglan wu', 'stu1102': 'longze Luola ', 'stu1103': 'xiaoze Maliya '}{}

 

Search:

Print ("stu1101" in id_dict) # in check whether this object is True # If yes, fFalse

 

Obtain:

Print (id_dict.get ("stu1101") # Use get to obtain the key value if it exists. If it does not exist, None, TengLan Wu is returned.
Print (id_dict ["stu1101"]) # This method will not be as intelligent as it is above. If the key does not exist, TengLan Wuprint will be reported directly (id_dict ["stu11231"]). print (id_dict ["studen31"]) KeyError: 'studen31'

 

Multi-dictionary nesting:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # File_type: Multi-dictionary nesting # Filename: dict_nest.py # Author: smelondid_dict = {"ChengDu ": {"acreage": 14312, "population": "1591.8 w", "postalcode": 610000}, "ShenZhen": {"acreage": 1196, "population ": "1190.08 w", "postalcode": 518000}, "BeiJing": {"acreage": "1.641 w", "population": "2172.9 w", "postalcode ": 100000} print (id_dict ["ChengDu"]) # print all output values
Print (id_dict ["ChengDu"] ["acreage"]) # print the area of the output ChengDu
Id_dict ["ChengDu"] ["acreage"] = "Area: 14312 square kilometers" # change the area of ChengDu to "Area: 14312 square kilometers"
Print (id_dict ["ChengDu"] ["acreage"]) # print

{'Acreage': 14312, 'population': '2017. 8w', 'postalcode': 1591}
14312
Area: 14312 square kilometers

 

Convert a dictionary to a tuples:

print(id_dict.items())dict_items([('stu1101', 'TengLan Wu'), ('stu1102', 'LongZe Luola'), ('stu1103', 'XiaoZe Maliya')])

 

Convert a dictionary to a list:

list_test = list(id_dict)print(list_test)['stu1101', 'stu1102', 'stu1103']

 

Dictionary cycle:

For key in id_dict: print (key, id_dict [key]) # as a key is added, stu is also cyclically output: stu1101 TengLan Wustu1102 LongZe Luolastu1103 XiaoZe Maliyafor key in id_dict: print (id_dict [key]) Output: TengLan WuLongZe LuolaXiaoZe Maliya

 

Multi-dictionary nested loop:

#! /Usr/bin/env python # _ * _ coding: UTF-8 _ * _ # File_type: Multi-dictionary nesting # Filename: dict_nest.py # Author: smelondid_dict = {"ChengDu ": {"acreage": 14312, "population": "1591.8 w", "postalcode": 610000}, "ShenZhen": {"acreage": 1196, "population ": "1190.08 w", "postalcode": 518000}, "BeiJing": {"acreage": "1.641 w", "population": "2172.9 w", "postalcode ": 100000} for key in id_dict: # Still loop test = id_dict [key] # assign each key to test for key in test: # loop test # print (key, ":", test [key], "\ n", end = "") print ("% s: % s "% (key, test [key]) # print the second-level key and the valueacreage: 14312 population: 1591.8 wpostalcode: 610000 acreage: 1196 population: 1190.08 wpostalcode: 518000 acreage: 1.641 wpopulation: 2172.9 wpostalcode: 100000

 

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.