序列
序列常用函数
可变序列:列表。
不可变序列:元组,字符串。
+
和*
可以用在任何序列。
增量赋值:在赋值的过程中同时进行计算。
python中每一个对象都有三个基本属性:唯一标志、类型和值。
唯一标志是随着对象创建的时候就有的,不可被修改,也不会有重复的。
id():返回一个代表指定对象的唯一标识的整数值。
>>> s = [1,2,3]
>>> id(s)
1839398209664
>>> s *= 2 #增量赋值
>>> s
[1, 2, 3, 1, 2, 3]
>>> id(s)
1839398209664 #可变序列的值改变时,id不会改变
>>> x = (1,2,3)
>>> id(x)
1839398251328
>>> x *= 2
>>> x
(1, 2, 3, 1, 2, 3)
>>> id(x)
1839397588704 #不可变序列的值改变时,其唯一标志id也改变
is
、is not
用于检测对象的id值是否相等,从而判断是否是同一个对象。
in
判断某个运算符是否包含在序列中。
not in
判断某个运算符是否不包含在序列中。
>>> "鱼" in "鱼C"
True
>>> "C"not in"FishC"
False
del
用于删除一个或多个指定的对象。
>>> x = [1,2,3,4,5]
>>> del x[1:4]
>>> x
[1, 5]
>>> y = [1,2,3,4,5]
>>> y[1:4] = [] #也可以用切片实现
>>> y
[1, 5]
>>> x = [1,2,3,4,5]
>>> del x[::2] #指定步长删除,不能用切片
>>> x
[2, 4]
>>> x = [1,2,3,4,5]
>>> x.clear()
>>> x
[]
>>> y = [1,2,3,4,5]
>>> del y[:] #清除列表内容
>>> y
[]
>>> del y #删除对象y
>>> y
Traceback (most recent call last):
File "<pyshell#34>", line 1, in <module>
y
NameError: name 'y' is not defined
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')
>>> str([1,2,3,4,5])
'[1, 2, 3, 4, 5]'
>>> str((1,2,3,4,5))
'(1, 2, 3, 4, 5)'
min():对比传入的参数并返回最小值。
max():对比传入的参数并返回最大值。
>>> s = [1,1,2,3,5]
>>> min(s)
1
>>> t = "FishC" #大写字母的编码值<大写字母的编码值(按26个英文字母的顺序排序)
>>> max(t) #如果是字符串,将会比较每个字符的编码值
's'
>>> s = []
>>> min(s)
Traceback (most recent call last):
File "<pyshell#47>", line 1, in <module>
min(s)
ValueError: min() arg is an empty sequence
>>> min(s,default="屁,啥都没有,怎么找到最小?") #若参数为空,则输出default的值
'屁,啥都没有,怎么找到最小?'
>>> min(1,2,3,0,6)
0
>>> max(1,2,3,0,6)
6
len():32位平台的最大值(231)-1,64位平台的最大值(263)-1
>>> len(range(2**100))
Traceback (most recent call last):
File "<pyshell#53>", line 1, in <module>
len(range(2**100))
OverflowError: Python int too large to convert to C ssize_t
sum():求和函数。
>>> s = [1,0,8,6]
>>> sum(s)
15
>>> sum(s,start=100) #start参数指定起始值
115
sorted():从小到大排序,不改变对象原来的值。
列表的sort()方法只能处理列表,而sorted()函数能处理任何形式的可迭代参数。
>>> s = [1,2,3,0,6]
>>> sorted(s) #sorted()函数不会改变对象的值
[0, 1, 2, 3, 6]
>>> s
[1, 2, 3, 0, 6]
>>> s.sort() #sort()方法会改变对象的值
>>> s
[0, 1, 2, 3, 6]
>>> s = [1,2,3,0,6]
>>> sorted(s,reverse=True)
[6, 3, 2, 1, 0]
>>> s
[1, 2, 3, 0, 6]
>>> s.sort(reverse=True)
>>> s
[6, 3, 2, 1, 0]
>>> t = ["FidhC","Apple","Book","Banana","Pen"]
>>> sorted(t)
['Apple', 'Banana', 'Book', 'FidhC', 'Pen'] #以编码值排序
>>> sorted(t,key=len)
['Pen', 'Book', 'FidhC', 'Apple', 'Banana'] #以len的值排序
>>> t.sort(key=len)
>>> t
['Pen', 'Book', 'FidhC', 'Apple', 'Banana']
reversed():返回一个迭代器,也是可迭代对象。
>>> s = [1,2,5,8,0]
>>> reversed(s)
<list_reverseiterator object at 0x000001AC44AE2190> #迭代器
>>> list(reversed(s)) #把迭代器转换为列表输出
[0, 8, 5, 2, 1]
>>> list(reversed("FishC"))
['C', 'h', 's', 'i', 'F']
>>> list(reversed((1,2,5,9,3)))
[3, 9, 5, 2, 1]
>>> list(reversed(range(0,10)))
[9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
all():判断可迭代对象中是否所有元素的值都为真。
any():判断是否存在某个元素的值为真。
>>> x = [1,1,0]
>>> y = [1,1,9]
>>> all(x)
False
>>> all(y)
True
>>> any(x)
True
>>> any(y)
True
enumerate()函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从0开始的序号共同构成一个二元组的列表。
>>> seasons = ["Spring","Summer","Fall","Winter"]
>>> enumerate(seasons)
<enumerate object at 0x000001AC44A35900>
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(seasons,10))
[(10, 'Spring'), (11, 'Summer'), (12, 'Fall'), (13, 'Winter')]
zip()函数用于创建一个聚合多个可迭代对象的迭代器。它会将作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第i个元组包含来自每个参数的第i个元素。
>>> x = [1,2,3]
>>> y = [4,5,6]
>>> zipped = zip(x,y)
>>> zipped
<zip object at 0x000001D0F63B2700>
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> z = [7,8,9]
>>> zipped = zip(x,y,z)
>>> list(zipped)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
>>> z = "FishC"
>>> zipped = zip(x,y,z)
>>> list(zipped)
[(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')]
map()函数,会根据提供的函数,对指定的可迭代对象的每个元素进行运算,并且返回运算结果的迭代器。
ord:传一个参数,求出对应的Unicode编码。
例如以下数字对应的是FishC每一个字母的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]
filter()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回。
简单来说,map()函数返回的是包含计算结果的迭代器,filter()函数返回计算结果为真的元素构成的迭代器。
str.islower():参数为小写字符返回True,大写返回False。
>>> list(filter(str.islower,"FishC"))
['i', 's', 'h']
迭代器&可迭代对象
一个迭代器肯定是一个迭代对象。
可迭代对象可以重复使用,而迭代器是一次性的。
>>> 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#22>", 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,"没有元素可提取啦~")
'没有元素可提取啦~'
>>> next(z,"没有元素可提取啦~")
'没有元素可提取啦~'
>>> next(z,"没有元素可提取啦~")
'没有元素可提取啦~'