在Python中从一个集合中移除元素的方法

202 阅读4分钟

我们在python中使用集合来存储唯一的不可变的对象。在这篇文章中,我们将讨论如何在python中从一个集合中移除元素。为此,我们将讨论使用不同方法的四种方法。

目录

使用pop()方法从一个集合中移除元素

我们可以使用pop()方法来从一个集合中移除元素pop() 方法,当对一个集合调用时,不接受任何输入参数。执行后,它从集合中删除一个随机元素并返回该元素。你可以在下面的例子中观察到这一点。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
element = mySet.pop()
print("The output set is:", mySet)
print("The popped element is:", element)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
The output set is: {2, 3, 4, 5, 6}
The popped element is: 1

pop() 方法只在非空的集合上运行良好。如果我们在一个空集合上调用pop() 方法,它将引发KeyError 异常,如下图所示。

mySet = set()
print("The input set is:", mySet)
element = mySet.pop()
print("The output set is:", mySet)
print("The popped element is:", element)

输出。

The input set is: set()
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
    element = mySet.pop()
KeyError: 'pop from an empty set'

pop() 方法从集合中删除了一个随机元素。如果我们必须要移除一个特定的元素,我们可以使用下面讨论的方法。

使用remove()方法从一个集合中移除元素

要从一个集合中移除一个指定的元素,我们可以使用remove() 方法。 remove() 方法,当对一个集合调用时,需要一个元素作为输入参数,并从集合中删除该元素,如下图所示。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
mySet.remove(3)
print("The output set is:", mySet)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
The output set is: {1, 2, 4, 5, 6}

如果作为输入传给remove() 方法的元素不存在于集合中,程序将遇到一个KeyError 异常,如下图所示。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
mySet.remove(7)
print("The output set is:", mySet)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
    mySet.remove(7)
KeyError: 7

同样地,如果我们试图用remove() 方法从一个空集合中删除一个元素,将会引发KeyError 异常。

mySet = set()
print("The input set is:", mySet)
mySet.remove(7)
print("The output set is:", mySet)

输出。

The input set is: set()
/usr/lib/python3/dist-packages/requests/__init__.py:89: RequestsDependencyWarning: urllib3 (1.26.7) or chardet (3.0.4) doesn't match a supported version!
  warnings.warn("urllib3 ({}) or chardet ({}) doesn't match a supported "
Traceback (most recent call last):
  File "/home/aditya1117/PycharmProjects/pythonProject/string1.py", line 3, in <module>
    mySet.remove(7)
KeyError: 7

使用discard()方法从一个集合中删除元素

当我们试图使用remove() 方法从一个集合中移除一个元素,而该元素并不存在于该集合中,程序将遇到一个KeyError 异常。你可以使用discard() 方法而不是remove() 方法来避免这种情况。discard() 方法,当在一个集合上调用时,接受一个元素作为输入参数,并像remove() 方法那样从集合中删除该元素。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
mySet.discard(3)
print("The output set is:", mySet)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
The output set is: {1, 2, 4, 5, 6}

然而,如果我们试图删除一个在集合中不存在的元素,discard()方法并没有引发异常。集合保持不变,程序也不会遇到KeyError 异常。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
mySet.discard(7)
print("The output set is:", mySet)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
The output set is: {1, 2, 3, 4, 5, 6}

使用clear()方法

clear() 方法是用来一次性删除任何给定集合中的所有元素。当在一个集合上调用时,clear() 方法不需要输入参数,并从该集合中删除所有的元素。你可以在下面的例子中观察到这一点。

mySet = {1, 2, 3, 4, 5, 6}
print("The input set is:", mySet)
mySet.clear()
print("The output set is:", mySet)

输出。

The input set is: {1, 2, 3, 4, 5, 6}
The output set is: set()

结论

在这篇文章中,我们讨论了在Python中从一个集合中移除元素的四种方法。