python之元组详细介绍
- 1.元组基本概念
- 2.元组操作
- 2.1 元组创建
- 2.2 元组解包
- 2.3 更新和删除元组
- 3.元组tuple和列表list的区别
- 3.1 元组tuple和列表list的相同点
- 3.2 元组tuple和列表list的不同点
1.元组基本概念
(1)定义:元组是⼀个不可变序列(⼀般当我们希望数据不改变时,我们使⽤元组,其他情况下基本都⽤列表)
(2)表现形式:tuple()
2.元组操作
2.1 元组创建
使用()创建元素
元组不是空元组⾄少有⼀个 逗号(,) 当元组不是空元组时括号可以省略
tuple函数将其他类型转为元组
my_tuple1 = () # 创建空列表
my_tuple2 = (1,2,3,4,5) # 创建int元素的列表
my_tuple3 = ('a','b','c','d') # 创建str元素的列表
my_tuple4 = ('a',1,True,None) # 创建混合列表
print(my_tuple1,my_tuple2,my_tuple3,my_tuple4)
my_tuple5 = 1,
print(type(my_tuple5)) #<class 'tuple'>
my_tuple6 = 1,2,3,4
print(type(my_tuple6)) #<class 'tuple'>
a = [1,2,3]
b = 'abc'
c = tuple(a)
d = tuple(b)
print(c,' ', type(c)) # tuple函数将列表转为元组,结果(1, 2, 3) <class 'tuple'>
print(d,' ', type(d)) # tuple函数将字符串转为元组,结果('a', 'b', 'c') <class 'tuple'>
2.2 元组解包
元组解包就是指将元组当中的每一个元素都赋值给变量
my_tuple = (1,2,3,4)
a,b,c,d = my_tuple
print('a =',a)
print('b =',b)
print('c =',c)
print('d =',d)
- 在元组解包时 变量的数量必须和元组中元素要一一对应
- 如果出现变量和元素没有对等的情况下,我们可以在变量前面加个*
- 这样变量就会讲获取元组中所有剩余的元素以列表形式返回
# 在元组解包时 变量的数量必须和元组中元素要一一对应
# 如果出现变量和元素没有对等的情况下,我们可以在变量前面加个*
# 这样变量就会讲获取元组中所有剩余的元素以列表形式返回
my_tuple = 10,20,30,40
# a,b,c = my_tuple # 报错。too many values to unpack (expected 3)
a,b,*c = my_tuple
print('a =',a) # a = 10
print('b =',b) # b = 20
print('c =',c) # c = [30,40]
a,*b,c = my_tuple
print('a =',a) # a = 10
print('b =',b) # b = [20,30]
print('c =',c) # c = 40
*a,b,c = my_tuple
print('a =',a) # a = [10, 20]
print('b =',b) # b = 30
print('c =',c) # c = 40
2.3 更新和删除元组
- 元组是不可以改变的。但是可以通过拷贝现有的元组片段构造一个新的元组的方式解决。
- 通过分片的方法让元组拆分成两部分,然后再使用连接操作符(+)合并成一个新元组,最后将原来的变量名(temp)指向连接好的新元组。在这里就要注意了,逗号是必须的,小括号也是必须的!
temp = ("龙猫","泰迪","叮当猫")
temp = temp[:2] + ("小猪佩奇",)+temp[2:]
print(temp) # 结果:('龙猫', '泰迪', '小猪佩奇', '叮当猫')
- 同理,删除元组中的元素:对于元组是不可变的原则来说,单独删除一个元素是不可能的,当然你可以利用切片的方式更新元组,间接的删除一个元素。
temp = ('龙猫', '泰迪', '小猪佩奇', '叮当猫')
temp = temp[:2] + temp[3:]
print(temp) # 结果:('龙猫', '泰迪', '叮当猫')
# 也可以通过del
temp = ('龙猫', '泰迪', '小猪佩奇', '叮当猫')
del temp
print(temp) #报错:
NameError: name 'temp' is not defined
3.元组tuple和列表list的区别
3.1 元组tuple和列表list的相同点
- in 和 not in
'''
# in 和 not in
# in 表示用来检查指定元素是否在列表当中,如果在返回True,如果不在返回False
# not in 表示用来检查指定元素是否不在列表当中,如果不在返回True,如果在返回False
'''
hero = ['钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长'] # 创建hero列表
print('钢铁侠' in hero) # 返回True
print('钢铁侠' not in hero) # 返回False
hero = ('钢铁侠','绿巨人','蜘蛛侠','黑寡妇','蚁人','美国队长') # 创建hero元组
print('钢铁侠' in hero) # 返回True
print('钢铁侠' not in hero) # 返回False
- python内置常用函数
- 元组调用方法
a = 5,2,3,4
print(a[0]) # 5
a = sorted(a,reverse = True)
print(a) # [5, 4, 3, 2]
print(a.count(1)) # 计算元素1出现的次数
print(a.index(5)) # 寻找元素5的索引
a = a.sort() # 排序,不能
print(a) # 输出为None
3.2 元组tuple和列表list的不同点
- 列表一旦创建了我们就可以根据自己的需要随意改变它的内容,我们可以给列边添加新的数据来增加其大小。
l = [1, 2, 3, 4, 5, 6]
l[0] = l[2] * l[3]
print(l) # [12, 2, 3, 4, 5, 6]
len(l) # 6
l.append(7) # [12, 2, 3, 4, 5, 6, 7]
len(l) # 7
- 元组是固定且不可改变的。这意味着一旦元组被创建,和列表不同,它的内容无法被修改或它的大小也无法被改变。
>>> t = (1, 2, 3, 4)
>>> t[0] = 5
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'tuple' object does not support item assignment
虽然它们不支持改变大小,但是我们可以将两个元组合并成一个新元组。这一操作类似列表的resize操作,但我们不需要为新生的元组分配任何额外的空间:
>>> t1 = (1, 2, 3, 4)
>>> t2 = (5, 6, 7, 8)
>>> t1 + t2
(1, 2, 3, 4, 5, 6, 7, 8)
一般情况下希望数据不改变,就用元组,其余情况都用列表