当前位置:网站首页>集合
集合
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.
集合生成式

边栏推荐
- 机器学习基础(三)——KNN/朴素贝叶斯/交叉验证/网格搜索
- MySql中的longtext字段的返回问题及解决
- Advanced application of C # language
- 开源 SPL 消灭数以万计的数据库中间表
- R language uses lubridate package to process date and time data
- How to convert word into PDF? Word to PDF simple way to share!
- Applet modification style (placeholder, checkbox style)
- Go语言 | 02 for循环及常用函数的使用
- 2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】
- JMeter 常用的几种断言方法,你会了吗?
猜你喜欢

2022最新大厂Android面试真题解析,Android开发必会技术

手把手教你处理 JS 逆向之图片伪装

cf:B. Almost Ternary Matrix【对称 + 找规律 + 构造 + 我是构造垃圾】

How to realize the Online timer and offline timer in the game

JMeter 常用的几种断言方法,你会了吗?

Reflection and imagination on the notation like tool

【AI 框架基础技术】自动求导机制 (Autograd)

微波雷达感应模块技术,实时智能检测人体存在,静止微小动静感知

What are the reliable domestic low code development platforms?

尚硅谷尚优选项目教程发布
随机推荐
Debezium系列之:postgresql从偏移量加载正确的最后一次提交 LSN
Hiengine: comparable to the local cloud native memory database engine
Ultrasonic ranging based on FPGA
彻底理解为什么网络 I/O 会被阻塞?
测试外包公司怎么样?
Why can't Bi software do correlation analysis? Take you to analyze
100million single men and women supported an IPO with a valuation of 13billion
How to quickly advance automated testing? Listen to the personal feelings of the three bat test engineers
Shell编程基础(第8篇:分支语句-case in)
flume系列之:拦截器过滤数据
JAD installation, configuration and integration idea
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
cf:B. Almost Ternary Matrix【對稱 + 找規律 + 構造 + 我是構造垃圾】
5. Data access - entityframework integration
为什么 BI 软件都搞不定关联分析?带你分析分析
What are the reliable domestic low code development platforms?
Debezium系列之:记录mariadb数据库删除多张临时表debezium解析到的消息以及解决方法
【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
如何在2022年更明智地应用智能合约?
Fuzor 2020軟件安裝包下載及安裝教程
