元组
有序不可变序列
1.创建元组给值
Python 的元组与列表类似,不同之处在于元组的元素不能修改。元组使用小括号,列表使用方括号。
元组创建很简单,只需要在括号中添加元素,并使用逗号隔开即可。也可以不加括号,一般加括号,便于识别
例:
y = (1,2,3,4,5)
print(type(y))
print(y)
t = 1,2,3,4,5
print(type(t))
print(t)
tup3 = "a", "b", "c", "d"
print (type(tup3))
print(tup3)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
(1, 2, 3, 4, 5)
<class 'tuple'>
('a', 'b', 'c', 'd')
- 一个元组作为另一个元组的元素
('hello', 16.0, 25,(1, 2, 3))
- 创建单个元素的元组,必须在末尾加一个逗号,未加则会将其作为int类型或str类型
例:
t = 1,
print(type(t))
print(t)
t = (1)
print(type(t))
print(t)
t = ('a')
print(type(t))
print(t)
<class 'tuple'>
(1,)
<class 'int'>
1
<class 'str'>
a
-使用内置函数tuple创建元组,也可以由其他类型转化为元组。如果不带参数,会创建一个空元组
例:
a = tuple()
print(a)
l = [1, 'a',3,4,5]
t = tuple(l)
print(type(t))
print(t)
s = "this is string!!!"
t = tuple(s)
print(type(t))
print(t)
()
<class 'tuple'>
(1, 'a', 3, 4, 5)
<class 'tuple'>
('t', 'h', 'i', 's', ' ', 'i', 's', ' ', 's', 't', 'r', 'i', 'n', 'g', '!', '!', '!')
- 列表类型转化元组,即把方括号变成圆括号。
- 字符串转化元组,把字符串每一个字符(包括特殊字符空格等)分隔出来作为元组中一个单独的元素
2、元组类型操作
元组数据值可以访问,不能修改,不能修改,不能修改
元组数据可以是任意类型
总之,list所有特性,除了可修改外,元组都具有
也就意味着,list具有的一些操作,比如索引,分片,序列相加,相乘,成员资格操作等,一模一样
- 元组可以使用下标索引来访问元组中的值
例:
tup1 = ('Google', 'Runoob', 1997, 2000)
tup2 = (1, 2, 3, 4, 5, 6, 7 )
print (tup1[0])
print (tup2[1:5])
Google
(2, 3, 4, 5)
- 元组中的元素值是不允许修改的,但我们可以对元组进行连接组合
tup1 = (12, 34.56)
tup2 = ('abc', 'xyz')
tup3 = tup1 + tup2 # 创建一个新的元组
tup4 =tup2 +tup1
print (tup3)
print (tup4)
(12, 34.56, 'abc', 'xyz')
('abc', 'xyz',12, 34.56)
- 元组中的元素值是不允许删除的,所以只能用del语句来删除整个元组
例:
tup = ('Google', 'Runoob', 1997, 2000)
del tup
3、元组运算符
len(tup) 计算元组中元素个数
len((1, 2, 3))
3
tup1 + tup2 连接元组tup1与元组tup2
例1:
(1, 2, 3) + (4, 5, 6)
(1, 2, 3, 4, 5, 6)
例2:
(4, 5, 6) +(1,2,3)
(4, 5, 6, 1, 2, 3)
tup*n 复制元组tup内元素
('Hi!',) * 4
('Hi!', 'Hi!', 'Hi!', 'Hi!')
<元素> in tup 查找元素是否在元组tup中,如果在其中即返回Ture,反之返回False
3 in (1, 2, 3)
Ture
for item in tup :
print(item) 元组tup遍历出每个元素
for x in (‘a’, 2, 3):
print (x,)
a
2
3
4、元组索引,截取,切片
使用<元组>[M:N:K]根据步长对字符串切片
表示<元组>M位置到N-1位置(即N位置的前一个位置),注意<元组>正向0开头
<元组>[M:N:K],M缺失表示至开头,N缺失表示至结尾,K缺失表示默认步长为1,步长为-1时,即倒序走
例:
l = ('Google', 'Taobao', 'Runoob')
print(l[2]) #读取第三个元素
print(l[-2]) #反向读取;读取倒数第二个元素
print(l[1:]) #截取元素,从第二个开始后的所有元素
print(l[::-1]) #步长-1,倒序输出
Runoob
Taobao
('Taobao', 'Runoob')
('Runoob', 'Taobao', 'Google')
5、元组内置函数
len(tuple) 计算元组元素个数
tuple1 = ('Google', 'Runoob', 'Taobao')
len(tuple1)
3
sum() 求列表元素之和,当列表元素为数字时才起作用
max() 求列表元素最大值
min() 求列表元素最小值
- 其他函数如max()、min()等对字符串元组和其他可进行比较的数据类型才会起作用。
例:
nums =[3,41,12,9,74,15]
print(len(nums))
print(max(nums))
print(min(nums))
print(sum(nums))
print(int(sum(nums)/len(nums)))
6
74
3
154
25
3、元组的比较
比较运算符适用于元组和其他序列,从每个序列的第一个元素开始比较。如果它们相等,以此类推,直到找到不同的元素
例:
(0,1,2) < (0,3,4)
True
('apple','b','c') > ('a','B','c')
True
4、元组排序处理
利用列表list转化,然后sort排序
例:
ls = (1,6,3,4,7,9,8,5,2)
lt =('A','F','apple','b','y')
a = list(ls)
b = list(lt)
a.sort() #只对其排序,不返回值
b.sort()
print(a)
print(b)
[1, 2, 3, 4, 5, 6, 7, 8, 9]
['A', 'F', 'apple', 'b', 'y']
利用sorted内置函数
例:
ls = (1,6,3,4,7,9,8,5,2)
lt =('A','F','apple','b','y')
print(sorted(ls)) #返回一个排序的新列表
print(sorted(lt))
[1, 2, 3, 4, 5, 6, 7, 8, 9]
['A', 'F', 'apple', 'b', 'y']
5、DSU模式:
修饰(Decorate) :在序列的元素之前放置一个或多个排序键,
-
排序(Sort) :使用python内置函数sort进行排序
-
去修饰(Undecorate) :提取出序列中已排序的元素
举例来说,有一组单词,对它们进行由长到短的排序:
txt = 'but soft what light in yonder window breaks'
words = txt.split() #将字符串个字符用空格隔开,并组成一个新的列表
print(words)
print(type(words))
t = list() #建立一个空列表t
for word in words:
t.append((len(word),word)) #遍历words列表,将单个单词字符串的列表每一个单词出现的次数与单词以元组列表形式给空列表t
print(t)
t.sort(reverse=True) #将t列表升序排序
res = list() #建立一个新列表res
for length, word in t:
res.append(word) #遍历t列表,将t列表中第二个元素word提出来再以此添加到列表res
print (res)
['but', 'soft', 'what', 'light', 'in', 'yonder', 'window', 'breaks']
<class 'list'>
[(3, 'but'), (4, 'soft'), (4, 'what'), (5, 'light'), (2, 'in'), (6, 'yonder'), (6, 'window'), (6, 'breaks')]
['yonder', 'window', 'breaks', 'light', 'what', 'soft', 'but', 'in']
当然,我们也可以利用split函数自动将字符串转换为单个字符串的列表的性质,再利用sort函数与lambda函数排序:
txt = 'but soft what light in yonder window breaks'
words = txt.split()
print(words)
print(type(words))
words.sort(key = lambda words : len(words), reverse=True)
print(word)
['but', 'soft', 'what', 'light', 'in', 'yonder', 'window', 'breaks']
<class 'list'>
['yonder', 'window', 'breaks', 'light', 'soft', 'what', 'but', 'in']
4.元组的赋值
元组可以出现在赋值语句的左侧,右侧可以是任何类型的序列,如字符串,列表或元组
赋值符号左右两侧元素个数必须一致,并且依次赋值,一次可以为多个变量赋值。
-当左侧是一个列表时:
例:
m = ['very','fun']
x,y = m
print(x)
print(type(x))
print(y)
print(type((x,y)))
print((x,y))
very
<class 'str'>
fun
<class 'tuple'>
('very', 'fun')
-当左侧是字符串时,需要利用split()函数转化为列表:
例:
m = 'I am very fun'
a,b,c,d = m.split()
print(a)
print(type(a))
print(type((a,b,c,d)))
print((a,b,c,d))
I
<class 'str'>
<class 'tuple'>
('I', 'am', 'very', 'fun')
-如果创建两个元组就会得到两个对象:
a = 1,2,3
b = 1,2,3
print(id(a))
print(id(b))
a is b
1870687951608
1870686825712
False
#两个元组等价,但不能说它们是同一个,它们是不同的对象
-如果a指向一个对象,当执行b = a后,两个变量都指向同一个对象:
a = ('I','am','very','fun','1')
print(id(a))
b = a
print(id(b))
a is b
1870687952544
1870687952544
True
#同一个对象有两个引用。拥有多个引用的对象就会有多个名称,这种现象称作对象被赋予了别名。
-但由其他类型转化而来的元组会事先建立一个地址,赋值饰只赋予元素而未赋予地址,所以不是同一个对象:
m = 'I am very fun'
print(id(m.split()))
a,b,c,d = m.split()
print(id((a,b,c,d)))
(a,b,c,d) is m.split()
1870688634760
1870688756744
False
-与列表不同的是, 但由其他类型转化而来的元组,初次传递与第二次传递后会得到三个不同的对象
m = 'I am very fun'
print(id(m.split()))
(a,b,c,d) = m.split()
tup = (a,b,c,d)
print(id((a,b,c,d)))
print(id(tup))
b = tup
print(id(b))
(a,b,c,d) is tup
1870688688712
1870688769192
1870688770232
1870688770232
False
-元组赋值有一个巧妙的用途,可以在一条语句中创建一个与左侧元组元素一样但元素顺序不同的新元组,并且要求左右侧元组元素个数必须一致:
强调:元组不可变
例1:
a = 1
b = 3
a,b = b,a
print(a)
print(b)
3
1
例2:
a,b,c,d,e,f = 1,2,3,4,5,6
b = b,c,d,e,a,f
print(b)
(2, 3, 4, 5, 1, 6)
5.字典与元组(详解例子见字典章节)
items() 方法以列表形式(并非直接的列表,若要返回列表值还需调用list函数)返回可遍历的(键, 值) 元组数组。
例:
d = {'a':10, 'b':1, 'c':22}
t = list(d.items())
print (t)
[('a', 10) , ('c', 22), ('b', 1)] #字典各元素的顺序不可测
-由于元组列表本身是一个列表,元组之间可以进行比较,以及对元组列表进行排序
例:
d = {'a':10, 'c':22, 'b':1}
t = list(d.items())
print (t)
t.sort()
print(t)
[('a', 10), ('c', 22), ('b', 1)]
[('a', 10), ('b', 1), ('c', 22)] #默认按元组第一个元素(即字典的键)的升序排序