一、序列(下)总结
all()函数 判断是否所有元素的值为真
any()函数 判断可迭代对象中是否某个元素为真
>>> x=[1,1,0]
>>> y=[1,1,9]
>>> all(x) #0为假
False
>>> all(y)
True
>>> any(y)
True
enumerate()函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从0开始的序号共同构成一个二元组的列表。
list(enumerate(seasons))
>>> seasons=["Spring","Summer","Fall","winter"]
>>> enumerate(seasons)
<enumerate object at 0x000002A412473090>
>>> list(enumerate(seasons)) #对列表中的元素标号,从0开始
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'winter')]
>>> list(enumerate(seasons,10)) #列表中的元素标号,从10开始
[(10, 'Spring'), (11, 'Summer'), (12, 'Fall'), (13, 'winter')]
>>>
3.zip()函数
zip()函数用于创建一个聚合多个可迭代对象的迭代器。
它会将作为参数传入的每个可迭代对象的每个元素依次组合成元祖,即第i个元祖包含来自每个参数的第i个元素。
>>> x=[1,2,3]
>>> y=[4,5,6]
>>> zipped=zip(x,y)
#返回三个元祖,每个元祖依次按下标索引0,1,2放到对应元祖中,也可以说是将每一列放到一个元祖里面。
>>> 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里面的h和c丢掉了。
>>> x=[1,2,3]
>>> y=[4,5,6]
>>> z="FishC"
>>> zipped=zip(x,y,z)
>>> list(zipped) #以长度最短的x,y为主
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's')]
>>>
itertools模块:
zip_longest()函数
>>> import itertools
# 传入可迭代对象长度不一致,但使用zip_longest()函数可以按最长的元素个数放入元祖里面,
# 尽管x,y没有但会将none放入。
>>> zipped=itertools.zip_longest(x,y,z)
>>> list(zipped)
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's'), (None, None, 'h'), (None, None, 'C')]
4.map()函数
map()函数 会将根据提供的函数对指定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器。
>>> mapped=map(ord,"FishC") #求字符串FishC的unicode编码
>>> list(mapped) #将FishC的编码结果用列表输出
[70, 105, 115, 104, 67]
>>>
>>> map(pow,[2,3,10],[5,2,3])
<map object at 0x000002A412471358>
#分别计算2的5次方,3的2次方,10的3次方。
>>> 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]
# 跟zip函数一样,都是在最短的可迭代对象终止的时候结束这个操作。
# 这里返回下标索引0,1,2所对应的最大元素2,3,9,而下标索引为4的元素不进行比较。
>>> list(map(max,[1,3,5],[2,2,2],[0,3,9,8]))
[2, 3, 9]
filter()函数 会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回。
map()函数返回的是包含计算结果的迭代器,而filter()函数返回的是计算结果为真的元素构成的迭代器。
>>> list(filter(str.islower,"FishC")) #过滤小写字母
['i', 's', 'h']
6.迭代器VS可迭代对象
一个迭代器肯定是一个可迭代对象,可迭代对象可以重复使用,而迭代器则是一次性的。
>>> mapped=map(ord,"FishC") #
>>> for each in mapped:
print(each)
70
105
115
104
67
>>> list(mapped) #mapped迭代器只能用一次,之后为空。
[]
使用 iter() 函数,就可以将一个可迭代对象转换成迭代器,然后使用 next() 函数访问容器中的每一个元素(一次只能访问一个)
iter()函数可以构成迭代器
next()函数可以将迭代器的内容依次显示出来
>>> x=[1,2,3,4,5]
>>> y=iter(x)
>>> type(x) #x是列表
<class 'list'>
>>> type(y) #y是列表迭代器
<class 'list_iterator'>
>>>
>>> next(y) #可以将迭代器的内容依次显示出来
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y) #迭代器里面没有任何元素时则会报错,抛出异常。
Traceback (most recent call last):
File "<pyshell#74>", line 1, in <module>
next(y)
StopIteration
next()里面加一个参数,迭代器里面元素为空时,不会抛出异常,则会显示这个参数(字符串)代替。
x=[1,2,3,4,5]
>>> z=iter(x)
>>> next(z,"没啦,被你掏空了")
1
>>> next(z,"没啦,被你掏空了")
2
>>> next(z,"没啦,被你掏空了")
3
>>> next(z,"没啦,被你掏空了")
4
>>> next(z,"没啦,被你掏空了")
5
>>> next(z,"没啦,被你掏空了")
'没啦,被你掏空了'
>>> next(z,"没啦,被你掏空了")
'没啦,被你掏空了'
课后题:
0. 请问下面代码会打印什么呢?
>>> x = [1, 2, 5, 8, 0]
>>> y = reversed(s)
>>> for each in y:
... print(each)
...
0
8
5
2
1
>>> list(y)
答:打印一个空列表。
解析:因为 reversed() 函数返回的结果是一个迭代器,迭代器是一次性的,里面的东西取了出来,就没有了。
1.请使用 map()函数,实现与下面代码相同的效果?
>>> [ord(x) for x in "FishC"]
[70, 105, 115, 104, 67]
答:
>>> list(map(ord, "FishC"))
[70, 105, 115, 104, 67]
2.请使用 map() 函数,对下面的二维列表进行排序?
>>> matrix = [[1, 3, 2],
... [5, 4, 6],
... [8, 7, 9]]
>>> # 这里利用好 map() 函数,一句代码的事情,就可以解决问题啦~
>>> # 排序后的结果如下
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
答:
>>> list(map(sorted, matrix))
[[1, 2, 3], [4, 5, 6], [7, 8, 9]]
list(map(sorted,[[1,3,2],[5,4,6],[8,7,9]])) #也可以
3.请问下面代码会打印什么呢?
>>> seasons = ["Spring", "Summer", "Fall", "Winter"]
>>> e = enumerate(seasons, start=11)
>>> print(e[2])
答:会报错。
解析:enumerate() 函数生成的是一个 enumerate 枚举对象,而非迭代器,所以不能使用下标对其进行随机访问。
4. 请使用列表推导式,实现与下面代码相同的结果。
>>> list(filter(str.islower, "FishC"))
['i', 's', 'h']
答:
>>> [i for i in "FishC" if i.islower()]
['i', 's', 'h']
5.下面代码试图同时对多个可迭代对象进行迭代操作,但是失败了……学了这一节课,你有没有办法解决这个问题?
>>> x = [1, 2, 3, 4, 5]
>>> y = "FishC"
>>> for i, j in x, y:
... print(i, j)
...
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
for i, j in x, y:
ValueError: too many values to unpack (expected 2)
答:
>>> for i, j in zip(x, y):
... print(i, j)
...
1 F
2 i
3 s
4 h
5 C
解析:利用 zip()函数,就可以成功实现一次性对多个可迭代对象进行迭代操作
6. 迭代器和可迭代对象最大的不同是?
答:可迭代对象可以重复使用,而迭代器则是一次性的。
>>> x = [1, 2, 3, 4, 5]
>>> y = iter(x)
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y)
Traceback (most recent call last):
File "<pyshell#52>", line 1, in <module>
next(y)
StopIteration
7. 所有可迭代对象都可以转换成迭代器吗?
答:是的,使用 iter() 函数,就可以将一个可迭代对象转换成迭代器,然后使用 next() 函数访问容器中的每一个元素(一次只能访问一个)。
题目来自小甲鱼python: 序列(下)