Introduction to Python Programming (013) - Dictionary Operations (2): Summary of Common Dictionary Operations

Introduction to Python Programming (013) - Dictionary Operations (2): Summary of Common Dictionary Operations

1. Get the value of the element in the dictionary

1. Use dictname[key]

The format is as follows:

dictname[key]

Note: If the key value does not exist in the dictionary, the program will report an error.

For example:

dict1 = {
    
    11:"Java",22:"Python",33:"C++",44:"C#",55:"Rust"}
print(dict1[11])
print(type(dict1[11]))
print(dict1[12])  # key不存在

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
Java
<class 'str'>
Traceback (most recent call last):
  File "C:\Python\Python38\First.py", line 4, in <module>
    print(dict1[12])  # key不存在
KeyError: 12

If you are not sure whether the key exists, you can do the following:

dict1 = {
    
    11:"Java",22:"Python",33:"C++",44:"C#",55:"Rust"}
if 11 in dict1:
    print(dict1[11])
    print(type(dict1[11]))
if 12 in dict1:
    print(dict1[12])
    print(type(dict1[12]))
else:
    print("key值不存在")  # key不存在
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
Java
<class 'str'>
key值不存在

2. Use the get() method

The syntax format of the get() method is as follows:

dictname.get(key)

Note: If the key value does not exist in the dictionary, None is returned.

as follows:

dict1 = {
    
    11:"Java",22:"Python",33:"C++",44:"C#",55:"Rust"}
print(dict1.get(11))
print(type(dict1.get(11)))
print(dict1.get(12))  # key值不存在
    
运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
Java
<class 'str'>
None

2. Add elements or modify value

1. Use dictname[key]

The format is as follows:

dictname[key] = vlaue

Note: Since the key in the dictionary is unique, if the key value does not exist in the dictionary, add a key-value pair; if the key value already exists in the dictionary, modify the value of value.

For example:

dict1 = {
    
    11:"Java",22:"Python",33:"C++",44:"C#",55:"Rust"}
dict1[66] = "MySQL"
print(dict1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    11: 'Java', 22: 'Python', 33: 'C++', 44: 'C#', 55: 'Rust', 66: 'MySQL'}

2. Use the update() method

The syntax format of the update() method is as follows:

dictname.update(key = value)

Description: If key already exists, update value. If key does not exist, add a new key-value pair.

For example:

dict1 = {
    
    "BookName":"Java从入门到精通","Author":"郑云","Price":59.8}
dict1.update(Price = 52.5)  # key值存在,更新对应的value值
print(dict1)  
dict1.update(Remark = "教材")  # key值不存在,添加一个新的键值对
print(dict1)  

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    'BookName': 'Java从入门到精通', 'Author': '郑云', 'Price': 52.5}
{
    
    'BookName': 'Java从入门到精通', 'Author': '郑云', 'Price': 52.5, 'Remark': '教材'}

3. Delete elements

1. Use pop() method to delete elements

Use the pop() method to delete the key-value pair corresponding to the specified key in the dictionary and return the deleted value. The syntax format of the pop() method is as follows:

dictname.pop(key[,default])

Parameter Description:

(1) key: Specify the key to be deleted.

(2) default: If the key to be deleted does not exist, return the default value.

(3) If the key to be deleted in the dictionary does not exist and the default parameter is not specified, a keyerror exception will be thrown.

For example:

dict1= {
    
    11:"Java",22:"Python",33:"C++",44:"Go",55:"Rust"}
item1 = dict1.pop(11,"要删除的值不存在")
print("pop()函数的返回值为:",item1)
print(type(item1))
print(dict1)
item1 = dict1.pop(12,"要删除的值不存在")
print("pop()函数的返回值为:",item1)
print(type(item1))
print(dict1)
item1 = dict1.pop(13)  # 出错
print("返回值为:",item1)
print(type(item1))
print(dict1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
pop()函数的返回值为: Java
<class 'str'>
{
    
    22: 'Python', 33: 'C++', 44: 'Go', 55: 'Rust'}
pop()函数的返回值为: 要删除的值不存在
<class 'str'>
{
    
    22: 'Python', 33: 'C++', 44: 'Go', 55: 'Rust'}
Traceback (most recent call last):
  File "C:\Python\Python38\First.py", line 10, in <module>
    item1 = dict1.pop(13)  # 出错
KeyError: 13

2. Use popitem() method to delete elements

Use the popitem() method to delete the last key-value pair in the dictionary and return this key-value pair as a tuple (key, values). The syntax format of the popitem() method is as follows:

dictname.popitem()

For example:

(): The return value is a tuple (key, values). By default, the voting file placed in asm is deleted and modified from back to front.

dict1 = {
    
    11:"Java",22:"Python",33:"C++",44:"C#",55:"Rust"}
item1 = dict1.popitem()
print("返回值:",item1)
print("返回值类型:",type(item1))
print(dict1)
item1 = dict1.popitem()
print(dict1)

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
返回值: (55, 'Rust')
返回值类型: <class 'tuple'>
{
    
    11: 'Java', 22: 'Python', 33: 'C++', 44: 'C#'}
{
    
    11: 'Java', 22: 'Python', 33: 'C++'}

3. Use del to delete the dictionary or elements in the dictionary.

(1) Delete dictionary

The format is as follows:

del dictname 
或
del(dictname) 

For example:

dict1 = {
    
    "BookName":"Java从入门到精通","Author":"郑云","Price":59.8}
del dict1
print(dict1) 

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
Traceback (most recent call last):
  File "C:\Python\Python38\First.py", line 3, in <module>
    print(dict1)
NameError: name 'dict1' is not defined

(2) Delete elements in the dictionary

del dict1[key]del(dict1[key]) 

For example:

dict1 = {
    
    "BookName":"Java从入门到精通","Author":"郑云","Price":59.8}
del dict1["Price"]
print(dict1)  

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    'BookName': 'Java从入门到精通', 'Author': '郑云'}

4. Merge dictionary

Use the update() method to merge the contents of one dictionary into another dictionary. The format is as follows:

dict1.update(dict2)   # 将dict2合并到dict1

For example:

dict1 = {
    
    "BookName":"Java从入门到精通","Author":"郑云"}
dict2 = {
    
    "Price":59.8,"Remark":"教材"}
dict1.update(dict2)
print(dict1)  

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
{
    
    'BookName': 'Java从入门到精通', 'Author': '郑云', 'Price': 59.8, 'Remark': '教材'}

5. Return the number of elements in the dictionary

Use the len() function to return the dictionary's element format. The format is as follows:

len(dictname)

For example:

dict1 = {
    
    "BookName":"Java从入门到精通","Author":"郑云","Price":59.8,"Remark":"教材"}
print(len(dict1)) 

运行结果如下:
===================== RESTART: C:\Python\Python38\First.py =====================
4

Guess you like

Origin blog.csdn.net/weixin_44377973/article/details/132171438