4.93Python数据类型之(8)集合
目录
目录
前言
集合是无序的,不可访问的,不可重复的元素的集合。用{}来标志的数据类型。
(一)基本概念
1.1有序于无序
有序与否的定义是:是否能够通过下标序列来访问元素。
例如:列表 list[2], string[0] ,tuple[0]等,是有序的。
而:集合是不可以通过下标来访问的,则是无序的。
1.2是否随机访问
能否访问某个元素。
1.3重复性
Python集合和数学里的集合类似,集合是互异的,即不可重复。
1.4可变与不可变的集合
可不可变的集合判断依据是:能否对其进行增删改。
(1)可变集合
创建有两种方式
- 直接用{},例如:s =
- 使用set()方法,例如:s=set(iterable) , iterable可迭代对象。
![01.png](https://upload-images.jianshu.io/upload_images/14032224-cdd088f956f6a03f.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)不可变集合
创建方式只有一种:
frozeset(iterabel), froze--冻结
![02.png](https://upload-images.jianshu.io/upload_images/14032224-388a3d170d356bc4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)集合的注意事项
空集合:使用 s = set()来创建,而不是用 s = {}
集合的元素要求:必须是不可变的类型。
(二)集合的增删改查
下面都是对于可变集合的操作。
2.1集合的增加
s.add(var) 方法
![03.png](https://upload-images.jianshu.io/upload_images/14032224-19a29cd188722618.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.2集合的删除
2.2.1remove()方法
remove()方法是根据某个元素来删除某个元素。当没有该元素时会报错。
![04.png](https://upload-images.jianshu.io/upload_images/14032224-d939f077e0c11a41.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.2.2discard()方法
与remove()方法操作和功能一样,唯一的区别是当没有该元素时,不做任何操作。
![05.png](https://upload-images.jianshu.io/upload_images/14032224-4e7126d389812a17.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.2.3pop()方法
pop()方法是从前面删除元素。并返回删除的元素。
![06.png](https://upload-images.jianshu.io/upload_images/14032224-3164b5774ea53d6a.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.2.4clear()方法
clear()方法是清空集合。
![07.png](https://upload-images.jianshu.io/upload_images/14032224-68568a3e0dfa6ff4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.3集合的修改
集合是不可变的,即不可更改。
2.4集合的查看
使用:for in s: 进行遍历查看。
![08.png](https://upload-images.jianshu.io/upload_images/14032224-a7a368d99702dba1.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
2.5不可变集合的查看
同样是用for i fs: 进行查看。
![09.png](https://upload-images.jianshu.io/upload_images/14032224-2617e58419a7a851.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(三)集合的运算
3.1集合的交集
s1.intersection(s2) 或者 "&"
![10.png](https://upload-images.jianshu.io/upload_images/14032224-175d58405c781a47.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
![11.png](https://upload-images.jianshu.io/upload_images/14032224-5ccfc025f912daae.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.2集合的并集
s1.union(s2) 或者 "|"
![13.png](https://upload-images.jianshu.io/upload_images/14032224-c7a82bd6ddaa17c4.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.3集合的差集
s1.defference(s2) 或者 "-"
![14.png](https://upload-images.jianshu.io/upload_images/14032224-2da2bd8d57ccedff.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
3.4集合的判断
(1)判断是否相交
s1.isdisjoint(s2)
![15.png](https://upload-images.jianshu.io/upload_images/14032224-ab6f0efc2d2a246d.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(2)判断是否包含
s1.issuperset(s2)
![16.png](https://upload-images.jianshu.io/upload_images/14032224-59e60be06a898e59.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
(3)判断是否包含于
s1.issubset(s2)
![17.png](https://upload-images.jianshu.io/upload_images/14032224-d40b37e9d0613877.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】凌霞软件回馈社区,博客园 & 1Panel & Halo 联合会员上线
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】博客园社区专享云产品让利特惠,阿里云新客6.5折上折
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步