📜  Python|从字典中删除键的方法

📅  最后修改于: 2022-05-13 01:54:35.865000             🧑  作者: Mango

Python|从字典中删除键的方法

Dictionary 还用于多种实际应用,例如日常编程、Web 开发和 AI/ML 编程,使其成为一个有用的容器。因此,了解实现与字典使用相关的不同任务的速记始终是一个加分项。本文处理这样一项任务,即从字典中删除字典键值对。

方法1:使用del

del关键字可用于就地删除字典中存在的键。可以考虑使用它的一个缺点是,如果找不到密钥,则会引发异常,因此必须处理不存在的密钥。代码 #1:演示使用del删除键值对

# Python code to demonstrate
# removal of dict. pair 
# using del
  
# Initializing dictionary
test_dict = {"Arushi" : 22, "Anuradha" : 21, "Mani" : 21, "Haritha" : 21}
  
# Printing dictionary before removal
print ("The dictionary before performing remove is : " + str(test_dict))
  
# Using del to remove a dict
# removes Mani
del test_dict['Mani']
  
# Printing dictionary after removal
print ("The dictionary after remove is : " + str(test_dict))
  
# Using del to remove a dict
# raises exception
del test_dict['Manjeet']

输出 :

The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}

例外 :

Traceback (most recent call last):
  File "/home/44db951e7011423359af4861d475458a.py", line 20, in 
    del test_dict['Manjeet']
KeyError: 'Manjeet'

方法 2:使用pop()

pop()可用于删除键及其值。使用del的优点是它提供了在尝试删除不存在的字典时打印所需值的机制。一对。其次,除了执行简单的删除操作之外,它还返回被删除的键的值。代码 #2:演示使用pop()删除键值对

# Python code to demonstrate
# removal of dict. pair 
# using pop()
  
# Initializing dictionary
test_dict = {"Arushi" : 22, "Anuradha" : 21, "Mani" : 21, "Haritha" : 21}
  
# Printing dictionary before removal
print ("The dictionary before performing remove is : " + str(test_dict))
  
# Using pop() to remove a dict. pair
# removes Mani
removed_value = test_dict.pop('Mani')
  
# Printing dictionary after removal
print ("The dictionary after remove is : " + str(test_dict))
print ("The removed key's value is : " + str(removed_value))
  
print ('\r')
  
# Using pop() to remove a dict. pair
# doesn't raise exception
# assigns 'No Key found' to removed_value
removed_value = test_dict.pop('Manjeet', 'No Key found')
  
# Printing dictionary after removal
print ("The dictionary after remove is : " + str(test_dict))
print ("The removed key's value is : " + str(removed_value))

输出 :

The dictionary before performing remove is : {'Arushi': 22, 'Anuradha': 21, 'Mani': 21, 'Haritha': 21}
The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : 21

The dictionary after remove is : {'Arushi': 22, 'Anuradha': 21, 'Haritha': 21}
The removed key's value is : No Key found

方法 3:使用items() + dict理解

items()与dict理解相结合也可以帮助我们完成键值对删除的任务,但是它的缺点是不是就地dict。技术。实际上,如果创建了一个新的字典,除了我们不希望包含的键。代码 #3:演示使用items() + dict 删除键值对。理解

# Python code to demonstrate
# removal of dict. pair 
# using items() + dict comprehension
  
# Initializing dictionary
test_dict = {"Arushi" : 22, "Anuradha" : 21, "Mani" : 21, "Haritha" : 21}
  
# Printing dictionary before removal
print ("The dictionary before performing remove is : " + str(test_dict))
  
# Using items() + dict comprehension to remove a dict. pair
# removes Mani
new_dict = {key:val for key, val in test_dict.items() if key != 'Mani'}
  
# Printing dictionary after removal
print ("The dictionary after remove is : " + str(new_dict))

输出 :

The dictionary before performing remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22, 'Mani': 21}
The dictionary after remove is : {'Anuradha': 21, 'Haritha': 21, 'Arushi': 22}