十六、序列
一、序列的共同点(列表、元素、字符串)
1、都可以通过索引获取每一个元素
2、第一个元素的索引值都是0
3、都可以通过切片的方法获取一个范围内的元素的集合
4、都有很多共同的运算符
5、可变序列:列表
不可变序列:元祖、字符串
二、序列运算符
1、+:拼接;*:拷贝
>>> [1,2,3]+[4,5,6]
[1, 2, 3, 4, 5, 6]
>>> (1,2,3)+(4,5,6)
(1, 2, 3, 4, 5, 6)
>>> "123"+"456"
'123456'
>>> [1,2,3]*3
[1, 2, 3, 1, 2, 3, 1, 2, 3]
>>> (1,2,3)*3
(1, 2, 3, 1, 2, 3, 1, 2, 3)
>>> "123"*3
'123123123'
#Python中每个对象都有三个基本属性,分别是唯一标识,类型和值,其中唯一标识是随着对象创建的时候就有的,是不可以被修改的,也不会有重复的值,函数id即返回一个代表指定对象的唯一标识的整数值。
#可变序列―列表(id在运算后不会改变)
>>> s=[1,2,3]
>>> id(s)
2064604609536
>>> s *= 2
>>> s
[1, 2, 3, 1, 2, 3]
>>> id(s)
2064604609536
#不可变序列―元祖(id在运算后会改变)
>>> t = (1,2,3)
>>> id(t)
2064647311232
>>> t *=2
>>> t
(1, 2, 3, 1, 2, 3)
>>> id(t)
2064647127392
2、同一性运算符is和is not:用于检测对象的id值是否相等,从而判断是否是同一个对象。
>>> x="Love"
>>> y="Love"
>>> x is y
True
>>> x=[1,2,3]
>>> y=[1,2,3]
>>> x is y
False
3、包含运算符in和not in:判断某元素是否包含在序列中。
>>> "鱼" in "鱼C"
True
>>> "C" not in "FishC"
False
4、del函数:用于删除一个或多个指定的对象,或删除可变序列中的指定元素
>>> x = "FishC"
>>> y = [1,2,3]
>>> del x,y
>>> x
Traceback (most recent call last):
File "<pyshell#30>", line 1, in <module>
x
NameError: name 'x' is not defined
>>> y
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
y
NameError: name 'y' is not defined
>>> x = [1,2,3,4,5]
>>> del x[1:4]
>>> x
[1, 5]
>>> x = [1,2,3,4,5]
>>> del x[::2]
>>> x
[2, 4]
>>> y = [1,2,3,4,5]
>>> y[1:4]=[]
>>> y
[1, 5]
#清空列表
>>> x = [1,2,3,4,5]
>>> x.clear()
>>> x
[]
>>> y = [1,2,3,4,5]
>>> del y[:]
>>> y
[]
三、序列内置函数
1、list()、tuple()、str():列表、元祖和字符串相互转换
>>> list("FishC")
['F', 'i', 's', 'h', 'C']
>>> list((1,2,3,4,5))
[1, 2, 3, 4, 5]
>>> tuple("FishC")
('F', 'i', 's', 'h', 'C')
>>> tuple([1,2,3,4,5])
(1, 2, 3, 4, 5)
>>> str([1,2,3,4,5])
'[1, 2, 3, 4, 5]'
>>> str((1,2,3,4,5))
'(1, 2, 3, 4, 5)'
2、min()和max()函数:对比传入的参数,并且返回最小值和最大值。
语法:
min(iterable,*[,key,default])
min(arg1,arg2,*args[,key])
max(iterable,*[,key, default])
max(arg1,arg2,*args[,key])
举例:
>>> s = [1,1,2,3,5]
>>> min(s)
1
>>> t ="FishC"
>>> max(t)
's'
>>> s=[]
>>> min(s)
Traceback (most recent call last):
File "<pyshell#59>", line 1, in <module>
min(s)
ValueError: min() arg is an empty sequence
>>> min(s,default="空值表")
'空值表'
#直接传入参数
>>> min(1,2,3,0,6)
0
>>> max(1,2,3,0,6)
6
3、len()函数:求字符的长度
len()函数参数的最大值,32位平台是2的31次方减1,64位平台是2的63次方减1
4、sum()函数:求和公式
>>> s=[1,0,0,8,6]
>>> sum(s)
15
>>> sum(s,start=100)
115
5、sorted()、reversed():对元素进行排序
>>> sorted(s)
[0, 1, 2, 3, 6]
#reversed表示翻转
>>> sorted(s,reverse=True)
[6, 3, 2, 1, 0]
>>> t = ["FishC","Apple","Book","Banana","Pen"]
>>> sorted(t)
['Apple', 'Banana', 'Book', 'FishC', 'Pen']
#key可以传入函数,比如比较字符的长度然后排序
>>> sorted(t,key=len)
['Pen', 'Book', 'FishC', 'Apple', 'Banana']
注意:sort是列表的函数,只能处理列表,但sorted可以处理字符串、元祖和列表,可接受任何形式的可迭代对象作为参数
>>> sorted("FishC")
['C', 'F', 'h', 'i', 's']
>>> sorted((1,0,0,8,6))
[0, 0, 1, 6, 8]
6、reversed()函数:调转元素,并返回的是一个参数的反向迭代器。迭代器:可迭代对象。reversed()函数要看到结果的话,需要变成列表或元祖或字符串。
>>> reversed(s)
<list_reverseiterator object at 0x000001E0B6912700>
>>> list(reversed(s))
[0, 8, 5, 2, 1]
>>> s.reverse()
>>> s
[0, 8, 5, 2, 1]
>>> list(reversed("FishC"))
['C', 'h', 's', 'i', 'F']
>>> list(reversed(range(0,10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
7、all()和any()函数
all()函数:判断可迭代对象中是否所有元素的值都为真
any()函数:判断可迭代对象中是否存在某个元素的值为真
>>> x = [1,1,0]
>>> y = [1,1,9]
>>> all(x)
False
>>> all(y)
True
>>> any(x)
True
>>> any(y)
True
8、enumerate()函数
enumerate()函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从0开始的序号共同构成一个二元组的列表。
>>> seasons = ["Spring","Summer","Fall","Winter"]
>>> enumerate(seasons)
<enumerate object at 0x000001E0B69113C0>
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
#start参数可以用来自定义这个序号开始的值
>>> list(enumerate(seasons,10))
[(10, 'Spring'), (11, 'Summer'), (12, 'Fall'), (13, 'Winter')]
9、zip()函数
zip()函数用于创建一个聚合多个可迭代对象的迭代器。
它会将作为参数传入的每个可迭代
对象的每个元素依次组合成元祖,即第i个元组包含来自每个参数的第i个元素。
>>> x = [1,2,3]
>>> y = [4,5,6]
>>> ziped =zip(s,y)
>>> ziped =zip(x,y)
>>> list(ziped)
[(1, 4), (2, 5), (3, 6)]
>>> z = [7, 8,9]
>>> ziped =zip(x,y,z)
>>> list(ziped)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
#传入的可迭代对象长度不一致时
>>> z = "FishC"
>>> ziped =zip(x,y,z)
>>> list(ziped)
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's')]
#itertools模块中有zip_longest()函数,补充长度不一致的元素
>>> import itertools
>>> zipped=itertools.zip_longest(x,y,z)
>>> list(zipped)
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's'), (None, None, 'h'), (None, None, 'C')]
10、map()函数
map()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器。
#ord求字符对应的Unicode编码
>>> mapped=map(ord,"FishC")
>>> list(mapped)
[70, 105, 115, 104, 67]
#pow计算次方
>>> mapped=map(pow,[2,3,10],[5,2,3])
>>> list(mapped)
[32, 9, 1000]
>>> [pow(2,5),pow(3,2),pow(10,3)]
[32, 9, 1000]
#长度不一致也是取最短
>>> list(map(max,[1,3,5],[2,2,2],[0,3,9,8]))
[2, 3, 9]
11、filter()函数
filter()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回。
>>> list(filter(str.islower,"FishC"))
['i', 's', 'h']
三、迭代器vs可迭代对象
一个迭代器肯定是一个可迭代对象,可迭代对象可以重复使用,而迭代器则是一次性的。
#举例一次性的迭代器
>>> mapped=map(ord,"FishC")
>>> for each in mapped:
print(each)
70
105
115
104
67
>>> list(mapped)
[]
#iter()函数:将可迭代对象变成一次性的迭代器
>>> x = [1,2,3,4,5]
>>> y = iter(x)
>>> type(x)
<class 'list'>
>>> type(y)
<class 'list_iterator'>
#next()函数针对迭代器,作用是逐个将迭代器中的元素给提取出来
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y)
Traceback (most recent call last):
File "<pyshell#131>", line 1, in <module>
next(y)
StopIteration
#不抛出异常
>>> z = iter(x)
>>> next(z,"没有值了")
1
>>> next(z,"没有值了")
2
>>> next(z,"没有值了")
3
>>> next(z,"没有值了")
4
>>> next(z,"没有值了")
5
>>> next(z,"没有值了")
'没有值了'