Python Set remove() 方法
Python 3 教程
· 2019-02-06 20:44:34
描述
remove() 方法用于移除集合中的指定元素。
该方法不同于 discard() 方法,因为
remove()
方法在移除一个不存在的元素时会发生错误,而 discard() 方法不会。
语法
remove() 方法语法:
set.remove(item)
参数
- item -- 要移除的元素
返回值
返回移除的元素。
实例
移除元素 banana:
实例 1
fruits = {"apple", "banana", "cherry"}
fruits.remove("banana")
print(fruits)
输出结果为:
{'cherry', 'apple'}
点击查看所有 Python 3 教程 文章: https://codercto.com/courses/l/10.html