Python 如何从集合中删除最后一个元素
在本文中,我们将学习如何从Python中的集合中删除最后一个元素。
使用的方法
以下是完成此任务的各种方法:
- 使用discard()函数
-
使用remove()函数
-
使用pop()函数
-
使用列表切片
方法1:使用discard()函数
discard() 函数通过将元素作为参数传递来从集合中移除元素。
步骤
以下是执行所需任务的算法/步骤。
- 创建一个变量来存储包含整数的输入集合。
-
使用 discard() 函数通过将集合的最后一个元素作为参数传递来从集合中删除最后一个元素。
-
打印从输入集合中删除最后一个元素后的结果集合。
-
以相同的方式,对包含字符串元素的集合进行检查。
示例
以下程序使用discard()函数从集合中删除最后一个元素:
# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# removing the last element from the set by passing the
# last element as an argument to the discard() function
inputSet.discard(8)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet)
# similarly removing the last element "python" from the set
inputSet_1.discard("python")
# printing the resultant set
print("Given Set after Removing Last Element", inputSet_1)
输出
执行以上程序后,将生成以下输出 –
The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {1, 2, 5, 7}
Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}
方法2:使用remove()函数
步骤
下面是执行所需任务的算法/步骤:
- 使用remove()函数通过将集合的最后一个元素作为参数传递给它来移除集合中的最后一个元素。
-
打印移除输入集合中最后一个元素后的结果集合。
示例
以下程序使用remove()函数从集合中移除最后一个元素:
# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# removing the last element from the set by passing the
# last element as an argument to the remove() function
inputSet.remove(8)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet)
# similarly removing the last element "python" from the set
inputSet_1.remove("python")
# printing the resultant set
print("Given Set after Removing Last Element", inputSet_1)
输出
执行上述程序后,将生成如下输出结果 –
The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {1, 2, 5, 7}
Given Set after Removing Last Element {'Hello', 'Tutorialspoint'}
方法3:使用pop()函数
在Python中,pop()方法从集合中删除随机元素。如果集合为空,则会引发错误。
使用pop()方法从输入集合中删除一个随机元素。
示例
以下程序使用pop()函数从输入集合中删除随机元素 –
# input set containing integers
inputSet = {5, 7, 2, 1, 8}
# removing random element from the set using pop()
inputSet.pop()
# printing the resultant set after removing any random element
print(inputSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
# similarly removing a random element from the set containing strings using pop()
inputSet_1.pop()
# printing the resultant set
print(inputSet_1)
输出
执行时,上述程序将生成以下输出-
{2, 5, 7, 8}
{'Hello', 'python'}
方法4:使用列表切片
步骤
以下是执行所需任务的算法/步骤:
- 使用 list() 函数将输入集合转换为列表(返回一个可迭代对象的列表),并使用切片删除集合中的最后一个元素,即取除最后一个元素外的所有元素,然后再次使用 set() 函数将结果转换为集合。
-
set()函数 - 创建一个集合对象。一个集合列表会以随机顺序出现,因为项目无序。它还会删除所有重复项。
-
打印从输入集合中删除最后一个元素后的结果集合。
示例
以下程序使用列表切片从集合中删除最后一个元素:
# input set containing integers
inputSet = {5, 7, 2, 1, 8}
print("The Given Set is:", inputSet)
# converting the input set to a list using list()
listOfSet = list(inputSet)
# Removing the last element using slicing i.e, taking all the elements except the last
resultList = listOfSet[:-1]
# Converting the result to a set
resultSet = set(resultList)
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", resultSet)
# input set containing strings
inputSet_1 = {"Hello", "Tutorialspoint", "python"}
print("The Given Set is:", inputSet_1)
# similarly removing the last element "python" from the set
# using list(),set() and slicing
resultSet_1 = set(list(inputSet_1)[:-1])
# printing the resultant set after removing the last element
print("Given Set after Removing Last Element", resultSet_1)
输出
在执行时,上述程序将生成以下输出结果−
The Given Set is: {1, 2, 5, 7, 8}
Given Set after Removing Last Element {1, 2, 5, 7}
The Given Set is: {'Tutorialspoint', 'Hello', 'python'}
Given Set after Removing Last Element {'Tutorialspoint', 'Hello'}
结论
在本文中,我们介绍了四种不同的方法来删除集合的最后一个元素。我们还学习了如何使用pop()函数从集合中删除一个随机元素,以及如何将集合转换为列表,反之亦然,以及如何对其进行切片。