python中的集合

一.集合的自动去重

集合里面的元素不可重复,所有重复元素会在输出时自动去重
python中的集合
运行结果:
python中的集合

二.定义一个空集合

如果想定义一个空集合,不能直接定义一个变量为空集合
需要将空列表转换为空集合。
python中的集合
运行结果:
python中的集合

三.利用集合快速完成列表去重

python中的集合
运行结果:
python中的集合

四.集合的常用方法

1.添加顺序和存储顺序是不一样的

python中的集合
运行结果:
python中的集合

2.添加元素 add:添加单个元素 update:添加多个元素

1.add:添加单个元素
python中的集合
运行结果:
python中的集合
2.添加多个元素
python中的集合
运行结果:
python中的集合

3.删除 pop:弹出 remove:删除

python中的集合
运行结果:
python中的集合

4.排序

python中的集合
运行结果:
python中的集合

5.union / |:并集

python中的集合
运行结果:
python中的集合

6.intersection / & :交集

python中的集合
运行结果:
python中的集合

7.difference / - :差集

python中的集合
运行结果:python中的集合

8.symmetric_difference / ^:对等差分(并集- 交集)

python中的集合
运行结果:
python中的集合

9.issubset:判断集合是否是另一个集合的子集

python中的集合
运行结果:
python中的集合

10.isdisjoint:判断两个集合是不是不相交

python中的集合
运行结果:
python中的集合

五.集合的特性

1.成员操作符

python中的集合
运行结果:
python中的集合

2.for循环

python中的集合
运行结果:
python中的集合