首页 > Python基础教程 > 列表、元组、字典和集合
阅读:4,632
Python set集合方法详解(全)
前面学习了 set 集合,本节来一一学习 set 类型提供的方法。首先,通过 dir(set) 命令可以查看它有哪些方法:
各个方法的具体语法结构及功能如表 1 所示。
>>> dir(set)
['add', 'clear', 'copy', 'difference', 'difference_update', 'discard', 'intersection', 'intersection_update', 'isdisjoint', 'issubset', 'issuperset', 'pop', 'remove', 'symmetric_difference', 'symmetric_difference_update', 'union', 'update']
各个方法的具体语法结构及功能如表 1 所示。
方法名 | 语法格式 | 功能 | 实例 |
---|---|---|---|
add() | set1.add() | 向 set1 集合中添加数字、字符串、元组或者布尔类型 |
>>> set1 = {1,2,3} >>> set1.add((1,2)) >>> set1 {(1, 2), 1, 2, 3} |
clear() | set1.clear() | 清空 set1 集合中所有元素 |
>>> set1 = {1,2,3} >>> set1.clear() >>> set1 set() set()才表示空集合,{}表示的是空字典 |
copy() | set2 = set1.copy() | 拷贝 set1 集合给 set2 |
>>> set1 = {1,2,3} >>> set2 = set1.copy() >>> set1.add(4) >>> set1 {1, 2, 3, 4} >>> set1 {1, 2, 3} |
difference() | set3 = set1.difference(set2) | 将 set1 中有而 set2 没有的元素给 set3 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set3 = set1.difference(set2) >>> set3 {1, 2} |
difference_update() | set1.difference_update(set2) | 从 set1 中删除与 set2 相同的元素 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set1.difference_update(set2) >>> set1 {1, 2} |
discard() | set1.discard(elem) | 删除 set1 中的 elem 元素 |
>>> set1 = {1,2,3} >>> set1.discard(2) >>> set1 {1, 3} >>> set1.discard(4) {1, 3} |
intersection() | set3 = set1.intersection(set2) | 取 set1 和 set2 的交集给 set3 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set3 = set1.intersection(set2) >>> set3 {3} |
intersection_update() | set1.intersection_update(set2) | 取 set1和 set2 的交集,并更新给 set1 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set1.intersection_update(set2) >>> set1 {3} |
isdisjoint() | set1.isdisjoint(set2) | 判断 set1 和 set2 是否没有交集,有交集返回 False;没有交集返回 True |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set1.isdisjoint(set2) False |
issubset() | set1.issubset(set2) | 判断 set1 是否是 set2 的子集 |
>>> set1 = {1,2,3} >>> set2 = {1,2} >>> set1.issubset(set2) False |
issuperset() | set1.issuperset(set2) | 判断 set2 是否是 set1 的子集 |
>>> set1 = {1,2,3} >>> set2 = {1,2} >>> set1.issuperset(set2) True |
pop() | a = set1.pop() | 取 set1 中一个元素,并赋值给 a |
>>> set1 = {1,2,3} >>> a = set1.pop() >>> set1 {2,3} >>> a 1 |
remove() | set1.remove(elem) | 移除 set1 中的 elem 元素 |
>>> set1 = {1,2,3} >>> set1.remove(2) >>> set1 {1, 3} >>> set1.remove(4) Traceback (most recent call last): File "<pyshell#90>", line 1, in <module> set1.remove(4) KeyError: 4 |
symmetric_difference() | set3 = set1.symmetric_difference(set2) | 取 set1 和 set2 中互不相同的元素,给 set3 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set3 = set1.symmetric_difference(set2) >>> set3 {1, 2, 4} |
symmetric_difference_update() | set1.symmetric_difference_update(set2) | 取 set1 和 set2 中互不相同的元素,并更新给 set1 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set1.symmetric_difference_update(set2) >>> set1 {1, 2, 4} |
union() | set3 = set1.union(set2) | 取 set1 和 set2 的并集,赋给 set3 |
>>> set1 = {1,2,3} >>> set2 = {3,4} >>> set3=set1.union(set2) >>> set3 {1, 2, 3, 4} |
update() | set1.update(elem) | 添加列表或集合中的元素到 set1 |
>>> set1 = {1,2,3} >>> set1.update([3,4]) >>> set1 {1,2,3,4} |
所有教程
- socket
- Python基础教程
- C#教程
- MySQL函数
- MySQL
- C语言入门
- C语言专题
- C语言编译器
- C语言编程实例
- GCC编译器
- 数据结构
- C语言项目案例
- C++教程
- OpenCV
- Qt教程
- Unity 3D教程
- UE4
- STL
- Redis
- Android教程
- JavaScript
- PHP
- Mybatis
- Spring Cloud
- Maven
- vi命令
- Spring Boot
- Spring MVC
- Hibernate
- Linux
- Linux命令
- Shell脚本
- Java教程
- 设计模式
- Spring
- Servlet
- Struts2
- Java Swing
- JSP教程
- CSS教程
- TensorFlow
- 区块链
- Go语言教程
- Docker
- 编程笔记
- 资源下载
- 关于我们
- 汇编语言
- 大数据
- 云计算
- VIP视频