📜  Python字典 pop() 方法

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

Python字典 pop() 方法

Python字典 pop()方法从字典中移除并返回指定元素。

Python Dictionary pop() 方法示例

示例 1:字典中弹出一个元素

Python3
# Python 3 code to demonstrate
# working of pop()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using pop to return and remove key-value pair.
pop_ele = test_dict.pop('Akash')
 
# Printing the value associated to popped key
print("Value associated to poppped key is : " + str(pop_ele))
 
# Printing dictionary after deletion
print("Dictionary after deletion is : " + str(test_dict))


Python3
# Python 3 code to demonstrate
# working of pop()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using pop to return and remove key-value pair.
pop_first = test_dict.pop("Nikhil")
 
# Printing the value associated to popped key
print("Value associated to poppped key is : " + str(pop_first))
 
# Printing dictionary after deletion
print("Dictionary after deletion is : " + str(test_dict))


Python3
# Python 3 code to demonstrate
# working of pop() without key
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using pop to return and remove key-value pair
# provided default
pop_ele = test_dict.pop('Manjeet', 4)
 
# Printing the value associated to popped key
# Prints 4
print("Value associated to poppped key is : " + str(pop_ele))
 
# using pop to return and remove key-value pair
# not provided default
pop_ele = test_dict.pop('Manjeet')
 
# Printing the value associated to popped key
# KeyError
print("Value associated to poppped key is : " + str(pop_ele))


输出 :

The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Value associated to poppped key is : 2
Dictionary after deletion is : {'Nikhil': 7, 'Akshat': 1}

示例 2: Python字典弹出第一个元素

Python3

# Python 3 code to demonstrate
# working of pop()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using pop to return and remove key-value pair.
pop_first = test_dict.pop("Nikhil")
 
# Printing the value associated to popped key
print("Value associated to poppped key is : " + str(pop_first))
 
# Printing dictionary after deletion
print("Dictionary after deletion is : " + str(test_dict))

输出:

The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Value associated to poppped key is : 7
Dictionary after deletion is : {'Akshat': 1, 'Akash': 2}

示例 3:弹出字典中不存在的元素

当键不在字典中时,pop()函数的行为是不同的。在这种情况下,它返回默认提供的值或 KeyError,以防即使没有提供默认值。

Python3

# Python 3 code to demonstrate
# working of pop() without key
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using pop to return and remove key-value pair
# provided default
pop_ele = test_dict.pop('Manjeet', 4)
 
# Printing the value associated to popped key
# Prints 4
print("Value associated to poppped key is : " + str(pop_ele))
 
# using pop to return and remove key-value pair
# not provided default
pop_ele = test_dict.pop('Manjeet')
 
# Printing the value associated to popped key
# KeyError
print("Value associated to poppped key is : " + str(pop_ele))

输出 :

The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Value associated to poppped key is : 4
Traceback (most recent call last):
  File "main.py", line 20, in 
    pop_ele = test_dict.pop('Manjeet')
KeyError: 'Manjeet'