Python 3 教程 Python Set discard() 方法

salvatore · 2022-03-14 15:03:39 · 热度: 13

描述

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

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

语法

discard() 方法语法:

set.discard(value)

参数

  • value -- 必需,要移除的元素

返回值

无。

实例

移除集合中的元素 banana:

实例 1

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

输出结果为:

{'cherry', 'apple'}

查看更多 Python 集合 内置函数

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