📜  Python字典 popitem() 方法

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

Python字典 popitem() 方法

Python字典 popitem() 方法从字典中删除最后插入的键值对并将其作为元组返回。

生成随机数在日常生活中有很多应用。在列表中,支持各种功能。整个库专用于Python来处理随机数。但有时,我们需要使用字典执行类似的任务。

注意:如果字典为空,popitem() 方法返回 keyError。

Python字典 popitem() 方法示例

示例 #1:演示 popitem() 的使用

在这里,我们将使用Python dict 来 popitem 在最后一个元素。

Python3
# Python 3 code to demonstrate
# working of popitem()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
# using popitem() to return + remove arbitrary
# pair
res = test_dict.popitem()
 
# Printing the pair returned
print('The arbitrary pair returned is : ' + str(res))
 
# Printing dict after deletion
print("The dictionary after removal : " + str(test_dict))


Python3
# Python 3 code to demonstrate
# application of popitem()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
n = len(test_dict)
 
# using popitem to assign ranks
for i in range(0, n):
    print("Rank " + str(i + 1) + " " + str(test_dict.popitem()))
 
# Printing end dict
print("The dictionary after deletion : " + str(test_dict))


Python3
# Python3 code to demonstrate working of
# Get random dictionary pair in dictionary
# Using popitem()
 
# Initialize dictionary
test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Get random dictionary pair in dictionary
# Using popitem()
res = test_dict.popitem()
 
# printing result
print("The random pair is : " + str(res))


输出 :

The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
The arbitrary pair returned is : ('Akash', 2)
The dictionary after removal : {'Nikhil': 7, 'Akshat': 1}

实际应用:这个特定的函数可以用来制定随机名称来玩游戏或决定随机排名列表,而无需使用任何随机函数。

示例 #2:演示 popitem() 的应用

Python3

# Python 3 code to demonstrate
# application of popitem()
 
# initializing dictionary
test_dict = {"Nikhil": 7, "Akshat": 1, "Akash": 2}
 
# Printing initial dict
print("The dictionary before deletion : " + str(test_dict))
 
n = len(test_dict)
 
# using popitem to assign ranks
for i in range(0, n):
    print("Rank " + str(i + 1) + " " + str(test_dict.popitem()))
 
# Printing end dict
print("The dictionary after deletion : " + str(test_dict))

输出 :

The dictionary before deletion : {'Nikhil': 7, 'Akshat': 1, 'Akash': 2}
Rank 1 ('Akash', 2)
Rank 2 ('Akshat', 1)
Rank 3 ('Nikhil', 7)
The dictionary after deletion : {}

示例 #3: Python字典 popitem random

Python3

# Python3 code to demonstrate working of
# Get random dictionary pair in dictionary
# Using popitem()
 
# Initialize dictionary
test_dict = {'Gfg' : 1, 'is' : 2, 'best' : 3}
 
# printing original dictionary
print("The original dictionary is : " + str(test_dict))
 
# Get random dictionary pair in dictionary
# Using popitem()
res = test_dict.popitem()
 
# printing result
print("The random pair is : " + str(res))

输出:

The original dictionary is : {'Gfg': 1, 'best': 3, 'is': 2}
The random pair is : ('is', 2)