关于列表方面请看: https://blog.csdn.net/weixin_41736752/article/details/96437875
2.元组
2.1特点
- 有序
- 可重复
- 不可更改
- 符号使用()
2.2 元组的创建
- 空元组的创建,不能添加元素
- 单元素元组的创建,需要在后面加逗号
>>> a=("a")
>>> type(a)
<class 'str'>
>>> a=('a',)
>>> type(a)
<class 'tuple'>
- 多元素元组的创建,包含多种数据类型
2.3拼接、重复、索引(偏移)、切片
同列表一样,不做详细测试
https://blog.csdn.net/weixin_41736752/article/details/96437875
2.4增删改查
查
- 索引查
- 切片查
- index() 返回位置
删
- 只能全部删除
- del 元组名即可
改
-
不能改元组,但是可以改元组中的列表
tp=('a','b','c',['a','b','c']) tp[3][1]='nice' #('a', 'b', 'c', ['a', 'nice', 'c'])
2.5基本函数
-
最大值最小值
max(tp) min(tp)
tp=(1,5,9,4,5,10) print(max(tp),min(tp)) #10 1
2.6遍历
-
元素遍历
tp=('one','two','and','a','b') for i in tp: print(i,end=' ')
-
索引遍历
tp=('one','two','and','a','b') for i in range(len(tp)): print(tp[i],end=' ')
-
枚举enumerate
tp=('one','two','and','a','b') for i in enumerate(tp,1): print(i,end=' ') #(1, 'one') (2, 'two') (3, 'and') (4, 'a') (5, 'b')
复习总结(字符串、列表、元组)
字符串、列表、元组的相同点和不同点
符号 | 引号“ ” | 方括号[ ] | 小括号() |
---|---|---|---|
特征 | 字符串 | 列表 | 元组 |
类型 | string | list | tuple |
顺序 | \ | 有序 | 有序 |
重复、索引、切片 | T | T | T |
增 | T | append()、extend()、insert(位置,元素) | F |
删 | s.strip(“指定字符”) | pop()、remove()、del、clear() | del |
改 | s.replace([现有],[替新],替换数量) | T | F |
查 | s.find()、s.index()、s.rindex()、 | T | index() |
特点 | 简单易操作 | 拷贝注意三种情况 | 不能对创建好的元组操作 |