如何在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()函数从集合中删除随机元素,以及如何将集合转换为列表,反之亦然,以及如何对其进行切片。
数据结构
网络
关系数据库管理系统(RDBMS)
操作系统
Java
iOS
HTML
CSS
Android
Python
C语言编程
C++
C#
MongoDB
MySQL
Javascript
PHP