Python3 dictionary
Dictionary is another variable container model, and can store any type of object.
A dictionary is a variable container that stores data in key/value pairs. The so-called variable means that the size of the container can be changed, and the elements can be modified or deleted. If you are familiar with Java, you will know that python's dictionary is similar. In Java's hashtable collection, each key-value (key=>value) pair of the dictionary is separated by a colon (:), and each pair is separated by a comma (, ). The entire dictionary is enclosed in braces {}, and JSON The format of is a bit similar, the declaration format is as follows:
d = {key1 : value1, key2 : value2 }
In the same dictionary, the key must be unique, but the value need not.
The value can take any data type, but the key must be immutable, such as a string, number, or tuple.
Example of declaration dictionary code:
dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
dict2 ={"age":20,"hight":170}
dict3 ={1:12.5,2:20,2.5:"test","one":12.23} #Different types can be used
print(dict1)print(dict2)print(dict3)
operation result:
{'name':'Zhang San','age': '20','address':'Hunan'}
{‘age’: 20, ‘hight’: 170}
{1: 12.5, 2: 20, 2.5: ‘test’, ‘one’: 12.23}
Use square brackets to access the value in the dictionary, put the key in the square brackets, you can get the value corresponding to the key, code example:
dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
dict2 ={"age":20,"hight":170}
dict3 ={1:12.5,2:20,2.5:"test","one":12.23} #Different types can be used
print(dict1["name"])print(dict2["hight"])print(dict3[1])
operation result:
Zhang San
170
12.5
**If you access a key that does not exist in the dictionary, an exception will be thrown, error example: **
dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}print(dict1["sex"])
operation result:
Traceback (most recent call last):
File “E:/PythonProject/TestDict1.py”, line 3, in
print(dict1[“sex”])
KeyError: ‘sex’
To add a new element to the dictionary, you only need to add a new key/value pair. To modify an existing value, you need to re-assign the value through the key. Code example:
dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
dict1["name"]="Li Si"print("Modify the value of the name key:", dict1)
dict1["sex"]="male"print("Added a key value:", dict1)
operation result:
Modified the value of the name key: {'name':'李四','age': '20','address':'Hunan'}
Added a key value: {'name':'李四','age': '20','address':'Hunan','sex':'Male'}
A single element can be deleted and all elements in the entire dictionary can be cleared. To clear it, you only need to call the clear() method.
Delete the dictionary object and delete a key value in the dictionary using the del command, code example:
dict1 ={"name":"Zhang San","age":"20","address":"Hunan"}
del dict1["name"] #Delete the name key value in the dictionary
del dict1 #Delete the entire dictionary object
dict1.clear() #Clear the elements in the dictionary, the dictionary object will not be deleted
The dictionary value can be any python object, either a standard object or a user-defined one, but the key does not work.
Two important points need to be remembered:
1 ) The same key is not allowed to appear twice. If the same key is assigned two or more times during creation, the value of the last key shall prevail. Code example:
dict1 ={"name":"Zhang San","age":"20","address":"Hunan","name":"Li Si"}print(dict1["name"])print(dict1)
operation result:
Li Si
{'name':'Li Si','age': '20','address':'Hunan'}
2 ) The key must be immutable, so it can be used as a number, string or tuple, but a list will not work. Examples of errors:
dict1 ={["name"]:"Zhang San","age":"20","address":"Hunan"}print(dict1)
operation result:
Traceback (most recent call last):
File “E:/PythonProject/TestDict2.py”, line 1, in
dict1 = {["name"]: "Zhang San", "age": "20", "address": "Hunan"}
TypeError: unhashable type: ‘list’
Recommended Posts