python元组
author:Once Day date:2022年2月17日
本文档在于总结相关内容,零散的知识难以记忆学习。
本文档基于windows平台。
全系列文档查看:python基础_CSDN博客。
1.创建
Python 的元组(tuple,简写为tup)与列表类似,不同之处在于元组的元素不能修改。
元组使用小括号()
,列表使用方括号[]
。元组也是序列结构,但是是一种不可变序列。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。
>>> (1,'s',[1,2,3])
(1, 's', [1, 2, 3])
>>> a=_ #_表示上一结果,是只读变量
>>> a
(1, 's', [1, 2, 3])
>>> type(a)
<class 'tuple'>
>>> emptyt=() #空元组
以上就可看出:元组里面可以嵌套其他对象,也包括元组本身。
需要注意创建一个元素的元组时:
>>> (50)
50
>>> ('sss')
'sss'
>>> (40,)
(40,)
>>>
需要在后面加一个逗号,否则会被当成()运算符,表示优先级较高。
可以把其他序列对象转化为元组:
>>> tuple([1,2,[12,3]])
(1, 2, [12, 3])
>>> tuple('14526')
('1', '4', '5', '2', '6')
2.访问元组
可以使用[索引]和切片访问!
2.1 索引访问
这是访问其中某个元素。具体方式和列表一样,除了不能修改一级元素。
#创建元组
>>> t=tuple('hello word')
>>> t
('h', 'e', 'l', 'l', 'o', ' ', 'w', 'o', 'r', 'd')
>>> t[1]
'e'
>>> t[-1]
'd'
>>> t[0]
'h'
>>> t[12] #超出范围访问
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
IndexError: tuple index out of range
>>> t[2]=5 #妄图改变数据
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
2.2 切片访问
切片操作是创建了一个新的副本。
>>> t=tuple('onceday')
>>> t
('o', 'n', 'c', 'e', 'd', 'a', 'y')
>>> t[:4]
('o', 'n', 'c', 'e')
>>> id(_)#查看内存地址
2481600595104
>>> id(t)
2481600507712
>>> _ is t
False
>>>
可以看到内存地址不一样,说明创建了新的对象。
具体的切片操作和列表一样。
>>> n=(1,2,3,4,5,6,7,8,9)
>>> n[2:4]
(3,4) #第一个索引指定开始(被包含),第二索引指定结尾,不被包含。
>>> n[7:9]
(8,9) #用虚构的第十个元素把最后一个元素包含进来。
>>> n[-3:-1]
(7,8) #可以使用复数索引。
>>> n[-3:0]
() #空序列,第二索引应在第一个索引后面。
>>> n[-3:]
(7,8,9) #省略第二个序列,则默认包含后面全部元素
>>> n[:2]
(1,2) #省略第一个索引,则默认包含前面全部元素,注意最后一个索引是不被包含的
>>> n[:]
(1,2,3,4,5,6,7,8,9) #包含所有元素
#改变步长,默认为1.
>>> n[0:9:2]
(1,3,5,7,9) #步长为2,每隔一个数取一个。
>>> n[9:0:-2]
(8,6,4,2) #从右到左提取元素,第一索引比第二索引大,但步长是负值,所以可以运行
>>> n[::-2]
(8,6,4,2) #也能成功!!!
2.3 元组相加连接:
用+连接同类型的序列!
>>> (1,2,3)+(4,5,6)
(1,2,3,4,5,6)
2.4 元组乘以数字
重复序列N次
>>> ('h','i')*5
('h', 'i', 'h', 'i', 'h', 'i', 'h', 'i', 'h', 'i')
2.5 成员资格
in 和not in运算符,布尔运算符,返回True或False
>>> t=tuple('onceday')
>>> t
('o', 'n', 'c', 'e', 'd', 'a', 'y')
>>> 'n' in t
True
>>> 'n' not in t
False
2.6 迭代元组
>>> t=tuple('onceday')
>>> t
('o', 'n', 'c', 'e', 'd', 'a', 'y')
>>> for x in t:print(x)
o
n
c
e
d
a
y
3.元组函数和方法
3.1 tuple() 将序列生成元组
>>> tuple([1,2,3])
(1, 2, 3)
>>> tuple('hellword')
('h', 'e', 'l', 'l', 'w', 'o', 'r', 'd')
3.1 len() 计算长度
>>> t=tuple('hello python')
>>> len(t)
12
3.2 max(),min()返回最大最小值
>>> t=tuple('hello python')
>>> t
('h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n')
>>> max(t)#按ascii编码值比较
'y'
>>> min(t)
' '
>>> t=(4,6,8)
>>> max(t)
8
3.3 count(value) 统计指定值的数量
>>> t=tuple('hello python')
>>> t
('h', 'e', 'l', 'l', 'o', ' ', 'p', 'y', 't', 'h', 'o', 'n')
>>> t.count('h')
2
>>> t=(1,4,2,5,2)
>>> t.count(2)
2
3.4 index(self, value, start=0, stop=9223372036854775807)查找指定值的序号
如果找不到值,将抛出错误ValueError。可指定开始和结束序号
>>> t=(1,4,2,5,2)
>>> t.index(2)
2 #当找到一个值后就不再查找了。
>>> t.index(8)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple
>>> t.index(2,0,2)
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
ValueError: tuple.index(x): x not in tuple
>>> t.index(2,0,3)
2
可以看到,查找列表不包含stop的位置。
3.5 del 删除元组
>>> t=(1,4,2,5,2)
>>> del t
>>> t
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
NameError: name 't' is not defined
直接删除整个对象。
4.元组注意事项
4.1 元组的不可变指的是元组所指向的内存中的内容不可变。
>>> t=tuple('hellp python')
>>> t
('h', 'e', 'l', 'l', 'p', ' ', 'p', 'y', 't', 'h', 'o', 'n')
>>> t=5
可以把指向元组标识符指向其他对象。
4.2 元组嵌套的二级元素内部不受元组不可变属性影响
>>> t=(1,2,'145152',[1,2,'45'])
>>> t
(1, 2, '145152', [1, 2, '45'])
>>> t[2][2]='s'
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'str' object does not support item assignment
>>> t[3][2]='s'
>>> t
(1, 2, '145152', [1, 2, 's'])
>>>
很明显,二级元素是否可变取决于二级元素自身能否可以修改!