当前位置:网站首页>6、组合数据类型
6、组合数据类型
2022-07-26 07:17:00 【亲自巡山的大王】
1 集合类型及操作
集合类型定义
集合用{}表示,元素之间用逗号分离
集合中间每个元素唯一,不存在相同元素
集合元素之间无序
集合操作符
并运算S|T、交运算S&T、差运算S-T、补运算S^T
关系操作符>,>=,<,<=
增强操作符:S ^= T,修改S
集合处理方法
S.add(x) #增加元素
S.discard(x) #删除元素,元素可以不是集合中的
S.remove(x) #移除元素,必须是在S中的
S.clear() #移除所有元素
S.pop() #随机取出(移除)一个元素,若空返回错误
S.copy()
len(S) #返回S中元素个数
x in S # 判断x是否在S中
x not in S
set(x) #将x类型转换为集合类型
集合类型应用场景
包含关系比较
数据去重:
ls = ['p', 'p', 'y', 'y', '123']
s = set(ls) # 利用了集合无重复元素特点
lt = list(s) # 将集合转换为列表
2 序列类型及其操作
序列:一维元素向量,类型可以不同,由序号引导
衍生:字符串、元组类型、列表类型
序列处理函数及方法
x in s # 判断是否在s内
x not in s
s + t # 连接两个序列
s * n 或 n * s # 将s复制n次
s[i] # 索引
s[i:j]或s[i:j:k] #切片操作,k是步长
取反操作:[::-1]
len(s):返回长度
min(s)
max(s) # 必须是可比较
s.index(x) 或s.index(x, i, j)# 第一个x出现的下标,整个序列或从i到j
s.count(x) # x出现的次数
元组类型及操作
元组一旦被创建不能被修改,可以使用小括号也可不使用
return 1, 2
列表类型
列表类型创建后可修改,用[]或list()创建,元素间用逗号分隔,元素类型可以不同,五长度限制。
ls[i] = x
ls[i:j:k] = lt
del ls[i]
del ls[i:j:k]
ls += lt
ls * n
ls.append(x) # 列表末尾增加一个元素x
ls.clear()
ls.copy()
ls.insert(i, x) # 在第i个位置后增加元素x
ls.pop(i) # 取出第i位置元素并删除
ls.remove(x) # 第一个x删除
ls.reverse() # 将列表元素反转
序列类型应用场景
对数据的表达
转换为元组类型:lt = tuple(ls)
边栏推荐
- Performance test -- lockust performs seckill business scenario test
- Flame diagram analysis Flink backpressure
- Deep learning learning notes -- solve the problem of slow download of CONDA and pip
- 如何对C盘进行扩容重新分区?
- Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !
- Drools(3):Drools基础语法(1)
- NPM command
- Unity3d asynchronous loading of scenes and progress bar loading
- Event loop in browser
- 倒计时2日!基于 Apache DolphinScheduler&TiDB 的交叉开发实践,从编写到调度让你大幅提升效率
猜你喜欢

Drools(3):Drools基础语法(1)
![From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.](/img/38/24c2e7cfcf794d09fd4d7b86b53728.png)
From scratch, we will completely develop an online chess game [Gobang] Based on websocket, and only use dozens of lines of code to complete all the logic.

单身杯web wp

Contents mismatch at: 08000000H (Flash=FFH Required=00H) ! Too many errors to display !

Common CMD instructions

20220725 自动控制原理中的补偿器

Hands on practice - teach you how to make an intelligent fish tank with STM32
![Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]](/img/21/c35b1493a36c46a89db0c6ebcf46db.jpg)
Talent column | can't use Apache dolphin scheduler? The most complete introductory tutorial written by the boss in one month [3]

Question: can't download sh shellcheck Please install it manually and some commands of shell script

Apache DolphinScheduler 2.X保姆级源码解析,中国移动工程师揭秘服务调度启动全流程
随机推荐
7月消息,Glassnode数据显示,Deribit上ETH永续期货合约未平仓头寸刚刚达到一个月高点237,959,827美元。
Manifest merger failed with multiple errors, see logs
Precious metal knowledge: lethal short-term secret script
基于C51实现led流水灯
Lite actor: lightweight optimization of ark actor concurrency model
Typora免费版下载安装
College degree sales career, from the third tier 4K to the first tier 20k+, I am very satisfied with myself
Introduction to C language -- summary table of operator priority sorting
Manifest merger failed with multiple errors, see logs
【QT】怎样获得QTableView和QTableWidget的行数和列数
Yolov6 target detection practice: training your own data set (video tutorial)
Leetcode 1184:公交站间的距离
如何对C盘进行扩容重新分区?
Flame diagram analysis Flink backpressure
404 page best practices to improve user experience
中国联通改造 Apache DolphinScheduler 资源中心,实现计费环境跨集群调用与数据脚本一站式访问
ModuleNotFoundError: No module named ‘pip‘解决办法
C51与MDK共存 Keil5安装教程
Drools (2): drools quick start
Now the metauniverse is no longer an existence that is only applied to the social field