Home  >  Article  >  Backend Development  >  One of the commonly used methods in Python dictionary

One of the commonly used methods in Python dictionary

WBOY
WBOYforward
2023-05-13 21:19:041498browse

One of the commonly used methods in Python dictionary

Dictionary is a commonly used data structure provided by Python, which is used to store data with mapping relationships. It is a mutable container model and can store any type of object. A dictionary is an unordered, mutable, and indexed collection. In Python, a dictionary is written with curly braces {} and consists of key-value pairs, namely key and value. Each key-value pair in the dictionary is separated by colon : and each key-value pair is separated by comma , . The keys in the dictionary are unique. If the key is repeated, the value corresponding to the subsequent key will replace the value corresponding to the previous key. The value can be any data type, but the key must be immutable, such as string, number or Tuples can be used as dictionary keys, but lists cannot be used as key values. eg:

dict1 = {"name":"张三","age":18,"地址":"China"}
dict2 = {"a":1,"b":2,"c":1,"d":3}
dict3 = {1:"a",2:"b",3:"c"}
dict4 = {}表示创建一个空的字典

1. Accessing values ​​in the dictionary

Accessing the dictionary in python is to access the values ​​in the dictionary through keys. If you access data with keys that are not in the dictionary, an error will be output.

eg:

dict1 = {"name":"张三","age":18,"地址":"China"}
m = dict1["name"]
print(m)

At this time, the printed result is "Zhang San", which means that the access dictionary key is the value corresponding to "name".

2. Access all keys in the dictionary Or all values

eg:

dict1 = {"name":"张三","age":18,"地址":"China"}
m = dict1.keys()
n = dict1.values()
print(m)
print(n)

At this time, the printed result is m for dict_keys(['name', 'age', 'address']), which obtains all the values ​​in the dictionary dict1 The value of the key. n is dict_values(['Zhang San', 18, 'China']), which obtains all the value values ​​in the dictionary dict1.

3. Modify the dictionary

Add a new key pair value in the dictionary, or modify the value corresponding to an existing key

Add a new key pair value:

dict1 = {"name":"张三","age":18,"地址":"China"}
dict1["成绩"]="优秀"
print(dict1)

The printed result at this time is {'name': 'Zhang San', 'age': 18, 'Address': 'China', 'Achievements': 'Excellent'}, and a key is added to dictionary dict1 as "Achievements" Key pair with value "excellent".

Modify the value corresponding to the existing key:

dict1={'name': '张三', 'age': 18, '地址': 'China', '成绩': '优秀'}
dict1["name"]="李四"
print(dict1)

The printed result is {'name': '李思', 'age': 18, 'Address': 'China', 'Achievements': 'Excellent'}, change the value corresponding to the key "name" in the dictionary dict1 from Zhang San to Li Si.

4. Delete dictionary elements The del() method can delete a single element or the dictionary. After deleting the dictionary, the dictionary will no longer exist

Delete a single element:

dict1={'name': '张三', 'age': 18, '地址': 'China', '成绩': '优秀'}

del dict1['成绩']
print(dict1)

The printed result at this time is {'name': 'Zhang San', 'age': 18, 'Address': 'China'}, and the key "score" and the corresponding value in the dictionary dict1 are deleted.

Delete dictionary:

dict1={'name': '张三', 'age': 18, '地址': 'China', '成绩': '优秀'}
del dict1
print(dict1)

At this time, the printed result is "NameError: name 'dict1' is not defined", which means that dictionary dict1 no longer exists because dictionary dict1 has been deleted.

5. Clear the dictionary. Because del is to delete the dictionary, if you want to clear the dictionary, you can use the dict.clear() method, which means to clear all key pairs in the dictionary.

eg:

dict1={'name': '张三', 'age': 18, '地址': 'China', '成绩': '优秀'}
dict1.clear()
print(dict1)

The printed result at this time is {}, which means that all values ​​in the dictionary dict1 have been cleared, and the dictionary dict1 is an empty dictionary.

The above is the detailed content of One of the commonly used methods in Python dictionary. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:51cto.com. If there is any infringement, please contact admin@php.cn delete