Python data type dictionary and python Data Type
Dictionary Definition
Dictionary is a key-value data type, which is very important and serves as a basis for distinguishing the use of lists and dictionaries.
Syntax format:
info = { 'stu1101': "Aaron", 'stu1102': "Jim", 'stu1103': "Jack",}
Dictionary features
- Dict is unordered
- The key must be unique, so the dictionary is inherently deduplicated.
- Values associated with key-value pairs can be numbers, strings, and lists.
Dictionary operation dictionary value access
>>> "Stu1102" in info # usage of standard search True >>> info. get ("stu1102") # get 'jim'> info ["stu1102"] # Same as above, but let's see the following 'Jim '>>> info ["stu1105"] # If a key does not exist, an error is returned. If the key does not exist, no error is returned. If the key does not exist, only NoneTraceback (most recent call last) is returned ): file "<stdin>", line 1, in <module> KeyError: 'stu1105'
Add dictionary Value
>>> info["stu1104"] = "West">>> print(info){'stu1102': 'Jim', 'stu1104': 'West', 'stu1103': 'Jack', 'stu1101': 'Aaron'}
Dictionary value Modification
>>> info["stu1103"] = "West">>> print(info){'stu1102': 'Jim', 'stu1104': 'West', 'stu1103': 'JacWest'}
Delete dictionary values
>>> Info {'stu1102': 'Jim ', 'stu1103': 'jack', 'stu1101': 'Aaron'} >>> info. pop ("stu1101") # standard Delete posture 'callon'> info {'stu1102 ': Jim', 'stu1103 ': 'jack' }>>> del info ['stu1103'] # Permanent deletion >>>> info {'stu1102': 'Jim '}>>> info = {'stu1102 ': 'Jim ', 'stu1103': 'jack'} >>> info {'stu1102': 'Jim', 'stu1103 ': 'jack'} # random deletion >>> info. popitem () ('stu1102', 'longze Luola ') >>> info {'stu1103': 'xiaoze Maliya '}
Dictionary Traversal
There are three types of traversal: Traverse Key-value pairs, and Traverse Key-value pairs. Note that all keys are traversed by default during dictionary traversal. In the following process, "for key in info. keys (): "can be changed to" for key in info: ", but the call method keys () is displayed, which makes the code more readable.
Info = {'stu1101': "Aaron", 'stu1102': "Jim", 'stu1103': "Jack",} # Traverse key-value pairs for key, value in info. items (): print ("\ nkey:" + key) print ("value:" + value) # output: key: stu1101value: onkey: stu1102value: Jimkey: stu1103value: jack # The traversal key for key in info. keys (): print ("key:" + key) # output: key: stu1101key: stu1102key: stu1103 # traverse the key value for value in info. value (): print ("value:" + value) # output: value: pythonvalue: Jimvalue: Jack
The dictionary is unordered according to the features of the dictionary. However, you need to obtain keys and key values in a specific order, which can be implemented using the sort method.
Info = {'stu1101': "Aaron", 'stu1102': "Jim", 'stu1103': "Jack",} for key in sorted (info. keys () print ("key:" + key) # output: key: stu1101key: stu1102key: stu1103
Dictionary nesting
The dictionary key value can be nested with numbers, lists, and dictionaries. Similarly, there are also list nesting. I am talking about nesting dictionaries in the list. The key to the specific nested data type is to grasp the characteristics of the list and dictionary. For the list, the element description in the list should correspond to the same attribute. For example, to describe the pizza dictionary, the pizza ingredients should not only be represented by a value, therefore, the key-value of this dictionary is a list, that is, a dictionary nested list. For the dictionary, it is good to understand the key-value pairs. They correspond to each other one by one. The difficulty in using dictionary Nesting is not how to operate, but whether a reasonable modeling model can be designed.
Dictionary nesting access
No matter who is nested, access to the list and dictionary is implemented through "[]". The only difference is the index or key in.
# Dictionary nested list pissa = {'crust ': 'thick', 'toprefers': ['mushrooms', 'extra ', 'cheese'],} print (pissa ['toppings '] [2]) # output: cheese # list nested dictionary lele_info = [{'name': 'Aaron', 'age ': '24'}, {'name': 'jim', 'age': '25'},] print (people_info [1] ['name']) # output: Jim