python学习笔记(集合)

知识点思维导图

python 复制代码
# 直接使用{}进行创建
s={10,20,30,40}
print(s)

# 使用内置函数set()创建
s=set()
print(s)

# 创建一个空的{}默认是字典类型
s={}
print(s,type(s))

s=set('helloworld')
print(s)
s=set([10,20,30])
print(s)
s1=set(range(1,10))
print(s1)

print('max:',max(s1))
print('min:',min(s1))
print('len:',len(s1))
python 复制代码
{40, 10, 20, 30}
set()
{} <class 'dict'>
{'h', 'l', 'w', 'r', 'd', 'e', 'o'}
{10, 20, 30}
{1, 2, 3, 4, 5, 6, 7, 8, 9}
max: 9
min: 1
len: 9

集合的操作

python 复制代码
a={1,2,3,4,5}
b={3,4,5,6,7}
print(a&b)  # 交集
print(a|b)  # 并集
print(a^b)  # 补集
print(a-b)  # 差集
python 复制代码
{3, 4, 5}
{1, 2, 3, 4, 5, 6, 7}
{1, 2, 6, 7}
{1, 2}

集合的遍历

python 复制代码
s={1,2,3,4,5,6,7,8,9}
for item in s:
    print(item)

for index,item in enumerate(s):
    print(index,"----",item)
python 复制代码
1
2
3
4
5
6
7
8
9
0 ---- 1
1 ---- 2
2 ---- 3
3 ---- 4
4 ---- 5
5 ---- 6
6 ---- 7
7 ---- 8
8 ---- 9

集合生成式

python 复制代码
s={i for i in range(1,10)}
print(s)

s={i for i in range(1,10) if i%2==1}
print(s)
python 复制代码
{1, 2, 3, 4, 5, 6, 7, 8, 9}
{1, 3, 5, 7, 9}

总结

相关推荐
AomanHao6 小时前
【阅读笔记】沙尘图像线性颜色校正A fusion-based enhancing approach for single sandstorm image
图像处理·笔记·isp·图像增强·沙尘图像·色偏·颜色校正
宇木灵8 小时前
C语言基础-十、文件操作
c语言·开发语言·学习
枷锁—sha8 小时前
【pwn系列】Pwndbg 汇编调试实操教程
网络·汇编·笔记·安全·网络安全
山岚的运维笔记9 小时前
SQL Server笔记 -- 第73章:排序/对行进行排序
数据库·笔记·后端·sql·microsoft·sqlserver
QQ243919711 小时前
语言在线考试与学习交流网页平台信息管理系统源码-SpringBoot后端+Vue前端+MySQL【可直接运行】
前端·spring boot·sql·学习·java-ee
眼镜哥(with glasses)11 小时前
0215笔记-语言模型,提问范式与 Token
人工智能·笔记·语言模型
FakeOccupational12 小时前
【电路笔记 通信】香农公式(Shannon-Hartley Theorem/香农-哈特利定理)证明(暂记)
笔记
The_Uniform_C@t213 小时前
论文浅读(第三期)|摘自《UAV Resilience Against Stealthy Attacks》(第一节)
网络·物联网·学习·网络安全
青衫码上行13 小时前
高频 SQL 50题(基础版)| 查询 + 连接
数据库·sql·学习·mysql
宇木灵13 小时前
C语言基础学习-X0前置
c语言·开发语言·学习