一篇文章带你了解python集合基础
作者:wakeyo_J 时间:2022-04-09 14:10:17
1. 集合类型定义
(1)集合是多个元素的无序组合
集合类型与数学中的集合概念一致
集合元素之间无序,每个元素唯一,不存在相同元素
集合元素不可更改,不能是可变数据类型
集合使用{}表示,元素间用英文逗号分隔
建立集合类型用{}或set()
建立空集合类型,必须使用set()
a = {'python',256,('sbvsb',251)}
print(a)
b = set('python')
print(b)
c = {'python',256,'python',256}
print(c)
2. 集合操作符
6个操作符
4个增强操作符
3. 集合处理方法
4. 集合类型应用场景
(1)包含关系比较
>>>'p' in {‘p','y','t','h'}
True
>>>{‘p','y'} <= {‘p','y','t','h'}
True
(2)数据去重:集合数据所有元素无重复
>>>ls = [‘p','p','y','y','t','h']
>>>s = set(ls)
{‘p','y','t','h'}
>>>ls = list(s)
[‘p','y','t','h']
来源:https://blog.csdn.net/m0_55697123/article/details/119190849
标签:python,集合,基础
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
python文件比较示例分享
2023-03-17 21:10:23
python的scipy实现插值的示例代码
2022-11-22 15:30:53
![](https://img.aspxhome.com/file/2023/4/93804_0s.png)
用 onerror 获取错误信息 js Debug
2008-11-03 19:08:00
PHP与Web页面交互操作实例分析
2023-09-05 14:43:16
![](https://img.aspxhome.com/file/2023/4/55604_0s.png)
python 使用百度AI接口进行人脸对比的步骤
2021-07-06 02:20:06
![](https://img.aspxhome.com/file/2023/7/68477_0s.png)
成功的用户界面的八个特性[译]
2009-04-20 20:36:00
![](https://img.aspxhome.com/file/UploadPic/20094/20/01-77s.jpg)
PHP children()函数讲解
2023-06-13 04:38:38
![](https://img.aspxhome.com/file/2023/9/55409_0s.png)
浅谈PyQt5中异步刷新UI和Python多线程总结
2023-08-22 01:37:18
优雅地扩大链接响应区域
2010-09-25 13:04:00
![](https://img.aspxhome.com/file/UploadPic/20109/25/2010-09-24_2300081-86s.png)
用PHP实现标准的IP Whois查询
2023-11-14 19:35:01
Python通过递归遍历出集合中所有元素的方法
2022-08-28 01:36:43
python 处理微信对账单数据的实例代码
2023-12-19 21:45:23
Python 用__new__方法实现单例的操作
2023-05-22 08:22:41
python 字典(dict)按键和值排序
2021-11-20 05:44:35
Python用摘要算法生成token及检验token的示例代码
2022-05-30 10:22:10
Python通过2种方法输出带颜色字体
2023-02-08 20:53:22
![](https://img.aspxhome.com/file/2023/7/71507_0s.png)
ASP教程:制作登陆验证页面程序
2008-10-23 15:00:00
Python基础之画图神器matplotlib
2022-03-03 23:34:02
![](https://img.aspxhome.com/file/2023/4/95414_0s.png)
pyinstaller将python程序打包为可执行文件
2022-06-05 17:11:35
![](https://img.aspxhome.com/file/2023/7/90377_0s.jpg)
Pytorch反向求导更新网络参数的方法
2021-02-07 11:48:52