当前位置:网站首页>集合
集合
2022-07-05 19:04:00 【51CTO】
集合的概念
1.Python语言提供的内置数据结构
2.与列表,字典一样属于可变类型的序列
3.集合是没有value的字典
集合的创建方式
1.直接花括号{}
2.使用内置函数set()
集合的存储不是顺序存储的,按照hash表的方式存储
3.空集合的创建方式
集合的相关操作
1.集合的判断操作
in / not in
print('--------------------集合的相关操作--------------------')
print('----判断操作----')
s = { 'python' , 'java' , 652}
#in
print('python' in s) #python在s中,True
print('cplusplus' in s) #cplusplus不在s中,False
#not
print( 'php' not in s) #php不在s中,True
print('java' not in s) #java在s中,False
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
2.集合的新增操作
调用add()方法,一次添加一个元素
调用update()方法,至少添加一个元素
3.集合的删除操作
调用remove()方法,一次删除一个指定元素,如果指定元素不存在抛出KeyError
调用discard()方法,一次删除一个指定元素,如果指定元素不存在抛出异常
调用pop()方法,一次只删除一个任意元素
调用clear()方法,清空集合
集合间的关系
1.两个集合是否相等
2.一个集合是否是另一个集合的子集
3.一个集合是否是另一个集合的超集
4.两个集合没有超集
集合的数学操作(交集,并集,差集,对称差集)
print('-------------集合的数学操作----------')
s1 = { 10,20,30,40 }
s2 = { 20,30,40,50,60 }
#交集 两个集合共有的元素
print(s1.intersection(s2))
print(s1 & s2)
#并集 两个集合的元素加起来(其中重复的元素只要一个)
print(s1.union(s2))
print(s1 | s2)
#差集 其中一个集合减去共有的元素,剩下的就是差集
print(s1.difference(s2))
print(s1 - s2)
#对称差集 两个集合都减去共有的元素,剩下的是对称差集
print(s1.symmetric_difference((s2)))
print(s1 ^ s2)
- 1.
- 2.
- 3.
- 4.
- 5.
- 6.
- 7.
- 8.
- 9.
- 10.
- 11.
- 12.
- 13.
- 14.
- 15.
集合生成式
边栏推荐
- 数据库 逻辑处理功能
- Go deep into the underlying C source code and explain the core design principles of redis
- 测试的核心价值到底是什么?
- 从外卖点单浅谈伪需求
- Debezium系列之:修改源码支持drop foreign key if exists fk
- Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
- 潘多拉 IOT 开发板学习(HAL 库)—— 实验8 定时器中断实验(学习笔记)
- cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
- #夏日挑战赛#数据库学霸笔记,考试/面试快速复习~
- 从零实现深度学习框架——LSTM从理论到实战【实战】
猜你喜欢
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
Analysis of postman core functions - parameterization and test report
After the company went bankrupt, the blackstones came
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
数据库 逻辑处理功能
UWB超宽带定位技术,实时厘米级高精度定位应用,超宽带传输技术
详解SQL中Groupings Sets 语句的功能和底层实现逻辑
如何在2022年更明智地应用智能合约?
HiEngine:可媲美本地的云原生内存数据库引擎
随机推荐
中国银河证券开户安全吗 证券开户
Mathematical modeling of oil pipeline layout MATLAB, mathematical model of oil pipeline layout
Postman核心功能解析 —— 参数化和测试报告
2022年5月腾讯云开发者社区视频月度榜单公布
Redhat7.4 configure Yum software warehouse (rhel7.4)
golang通过指针for...range实现切片中元素的值的更改
PG基础篇--逻辑结构管理(用户及权限管理)
2022最新大厂Android面试真题解析,Android开发必会技术
What are the reliable domestic low code development platforms?
华律网牵手观测云,上线系统全链路可观测平台
5 years of experience, 27 days of Android programmer interview, 2022 programmer advanced classic
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
Oracle fault handling: ora-10873:file * needs to be either taken out of backup or media recovered
R language uses lubridate package to process date and time data
JS解力扣每日一题(十二)——556. 下一个更大元素 III(2022-7-3)
面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】
618“低调”谢幕,百秋尚美如何携手品牌跨越“不确定时代”?
Optimization of middle alignment of loading style of device player in easycvr electronic map