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

边栏推荐
- Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
- Postman核心功能解析-参数化和测试报告
- Debezium系列之:解析默认值字符集
- 【历史上的今天】7 月 5 日:Google 之母出生;同一天诞生的两位图灵奖先驱
- MMO项目学习一:预热
- 手机开户选择哪家券商公司比较好哪家平台更安全
- Password reset of MariaDB root user and ordinary user
- Fuzor 2020 software installation package download and installation tutorial
- The binary string mode is displayed after the value with the field type of longtext in MySQL is exported
- How to convert word into PDF? Word to PDF simple way to share!
猜你喜欢

Blue sky drawing bed Apple quick instructions

Hiengine: comparable to the local cloud native memory database engine

Reflection and imagination on the notation like tool

Tutoriel de téléchargement et d'installation du progiciel fuzor 2020

华为让出的高端市场,小米12S靠徕卡能抢到吗?
MySql中的longtext字段的返回问题及解决

2022全网最全的腾讯后台自动化测试与持续部署实践【万字长文】

基于FPGA的超声波测距

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

14、用户、组和权限(14)
随机推荐
Ten years at sea: old and new relay, dark horse rising
JAD的安装、配置及集成IDEA
Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?
MySQL中字段类型为longtext的值导出后显示二进制串方式
如何快速进阶自动化测试?听听这3位BAT大厂测试工程师的切身感想....
#夏日挑战赛# HarmonyOS - 实现消息通知功能
在线协作产品哪家强?微软 Loop 、Notion、FlowUs
测试的核心价值到底是什么?
Talking about fake demand from takeout order
UDF implementation of Dameng database
Hiengine: comparable to the local cloud native memory database engine
从零实现深度学习框架——LSTM从理论到实战【实战】
华为让出的高端市场,小米12S靠徕卡能抢到吗?
Word finds red text word finds color font word finds highlighted formatted text
JAD installation, configuration and integration idea
Technology sharing | interface testing value and system
Oracle date format conversion to_ date,to_ char,to_ Timestamp mutual conversion
ELK分布式日志分析系统部署(华为云)
Mysql如何对json数据进行查询及修改
