- Python机器学习:数据分析与评分卡建模(微课版)
- 周晓然 翟锟 胡锋
- 188字
- 2025-02-26 19:27:54
2.4.2 集合的增、删
如果向集合中添加元素,则使用add()函数。集合的add()示例如图2-16所示。

图2-16 集合的add()示例
如果要随机删除集合中的一个元素,可以使用pop()方法;如果要删除指定的元素,可以使用remove()和discard()方法。但当remove()方法找不到指定的元素时,会报错;而当discard()方法找不到指定的元素时,并不会报错。如图2-17、图2-18和图2-19所示。

图2-17 集合的pop()示例

图2-18 集合的remove()示例

图2-19 集合的discard()示例