Python 3 教程 Python Set remove() 方法

cary · 2022-03-14 15:04:06 · 热度: 18

描述

remove() 方法用于移除集合中的指定元素。

该方法不同于 discard() 方法,因为 remove() 方法在移除一个不存在的元素时会发生错误,而 discard() 方法不会。

语法

remove() 方法语法:

set.remove(item)

参数

  • item -- 要移除的元素

返回值

没有返回值。

实例

移除元素 banana:

实例 1

fruits = {"apple", "banana", "cherry"} fruits.remove("banana") print(fruits)

输出结果为:

{'cherry', 'apple'}

查看更多 Python 集合 内置函数

猜你喜欢:
暂无回复。
需要 登录 后方可回复, 如果你还没有账号请点击这里 注册