小甲鱼 发表于 2021-7-28 23:49:41

已有 25 人购买  本主题需向作者支付 5 鱼币 才能浏览 购买主题

Kawhi_Chan 发表于 2021-7-29 11:56:15

公众号看到更新啦!

小古比鱼 发表于 2021-10-25 11:27:27

本节继续讲解与序列相关的内置函数,讲了all()、any()、enumerate()、zip()、map()、filter()、iter()和next()共8个函数,连同上一节讲过的9个函数,总共讲解了17个与序列相关的内置函数!其中,enumerate()函数和zip()函数有点意思,有种逐一打包的感觉!map()函数支持多个参数的运算处理,灵活方便!需要注意的是,zip()函数和map()函数在处理数据时是以最短的参数为准,可使用itertools模块中的zip_longest()函数来以最长的参数为准进行“打包”。视频中间还讲解了迭代器与可迭代对象的关系并举例说明,总结起来就是:一个迭代器肯定是一个可迭代对象;可迭代对象可以重复使用,而迭代器则是一次性的。iter()函数可将序列等可迭代对象转换为其所对应的迭代器。next()函数返回迭代器中的下一个元素,可通过设置default参数来提升用户体验,避免迭代终止时抛出StopIteration异常。

wangtuan 发表于 2021-12-4 13:56:12

>>> ["all() 函数用来判断可迭代对象中是否所有元素的值都为真, any() 函数判断可迭代对象中是否存在某个元素的值为真"]
['all() 函数用来判断可迭代对象中是否所有元素的值都为真, any() 函数判断可迭代对象中是否存在某个元素的值为真']
>>> x =
>>> y =
>>> all(x)
False
>>> all(y)
True
>>> any(x)
True
>>> any(y)
True
>>> ["enumerate() 函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从0开始的序号共同构成一个二元组的列表"]
['enumerate() 函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从0开始的序号共同构成一个二元组的列表']
>>> season = ["Spring","Summer","Fall","Winter"]
>>> enumerate(season)
<enumerate object at 0x000002BB821295C0>
>>> list(enumerate(saeson))
Traceback (most recent call last):
File "<pyshell#10>", line 1, in <module>
    list(enumerate(saeson))
NameError: name 'saeson' is not defined
>>> list(enumerate(season))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
>>> list(enumerate(season,10))
[(10, 'Spring'), (11, 'Summer'), (12, 'Fall'), (13, 'Winter')]
>>> ["10 所在的位置为 "start",即为序号开始的数"]
SyntaxError: invalid syntax
>>> ["10 所在的位置为 start,即为序号开始的数"]
['10 所在的位置为 start,即为序号开始的数']
>>> ["zip() 函数用于创建一个聚合多个可迭代对象的迭代器。它会将作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第i个元组包含来自每个参数的第i个元素"]
['zip() 函数用于创建一个聚合多个可迭代对象的迭代器。它会将作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第i个元组包含来自每个参数的第i个元素']
>>> x =
>>> y =
>>> zip(x,y)
<zip object at 0x000002BB82085340>
>>> zipped = zip(x,y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> z =
>>> 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')]
>>> ["如果使用 zip() 函数聚合的可迭代对象的长度不同,那么它会以最短的长度为准"]
['如果使用 zip() 函数聚合的可迭代对象的长度不同,那么它会以最短的长度为准']
>>> import itertools
>>> zipped = itertool.zip_longest(x,y,z)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
    zipped = itertool.zip_longest(x,y,z)
NameError: name 'itertool' is not defined
>>> zipped = itertools.zip_longest(x,y,z)
>>> list(zipped)
[(1, 4, 'F'), (2, 5, 'i'), (3, 6, 's'), (None, None, 'h'), (None, None, 'C')]
>>> ["还是上面那种情况,可以通过 引入 itertools 中的 zip_longest() 模块,即如上所示,会将所有元素展示,没有的用 None 代替"]
['还是上面那种情况,可以通过 引入 itertools 中的 zip_longest() 模块,即如上所示,会将所有元素展示,没有的用 None 代替']
>>> ["map() 函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器"]
['map() 函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器']
>>> mapped = map(ord,"FishC")
>>> list(mapped)

>>> ["ord返回每个字符的 unicode 编码"]
['ord返回每个字符的 unicode 编码']
>>> mapped = map(pow,,)
>>> list(mapped)

>>> ["即是 通过pow 函数计算 2的5次方、3的2次方、10的3次方"]
['即是 通过pow 函数计算 2的5次方、3的2次方、10的3次方']
>>>

>>> list(map(max,,,))

>>> ["同 zip() 函数一样,当可迭代对象的长度不一致时, map() 函数也是以长度最短的可迭代对象为准"]
['同 zip() 函数一样,当可迭代对象的长度不一致时, map() 函数也是以长度最短的可迭代对象为准']
>>> ["filter()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回"]
['filter()函数会根据提供的函数对指定的可迭代对象的每个元素进行运算,并将运算结果为真的元素,以迭代器的形式返回']
>>> list(filter(str.islower,"FishC"))
['i', 's', 'h']
>>> ["str.islower是判断字符串中的元素是否为小写,本次例子给出的 FishC 中,小写的只有 i s h ,因此,filter() 就会返回这三个结果,即返回真的结果"]
['str.islower是判断字符串中的元素是否为小写,本次例子给出的 FishC 中,小写的只有 i s h ,因此,filter() 就会返回这三个结果,即返回真的结果']
>>> ["1) 一个迭代器肯定是一个可迭代对象                                             2)可迭代对象可以重复使用,而迭代器则是一次性的"]
['1) 一个迭代器肯定是一个可迭代对象                                             2)可迭代对象可以重复使用,而迭代器则是一次性的']
>>> mapped = map(ord,"FishC")
>>> for each in mapped:
        print(each)

       
70
105
115
104
67
>>> list(mapped)
[]
>>> ["上述 mapped 迭代器经过一次 for 循环使用后,就会转化为空"]
['上述 mapped 迭代器经过一次 for 循环使用后,就会转化为空']
>>> x =
>>> y = iter(x)
>>> type(x)
<class 'list'>
>>> type(y)
<class 'list_iterator'>
>>> ["iter() 函数可以将可迭代对象转变 为 迭代器"]
['iter() 函数可以将可迭代对象转变 为 迭代器']
>>> next(y)
1
>>> next(y)
2
>>> next(y)
3
>>> next(y)
4
>>> next(y)
5
>>> next(y)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
    next(y)
StopIteration
>>> ["next() 函数专门用来提取迭代器中的元素,只不过是逐个提取,如果没有元素可以提取了,就会报错"]
['next() 函数专门用来提取迭代器中的元素,只不过是逐个提取,如果没有元素可以提取了,就会报错']
>>> z = iter(x)
>>> next(z,"没啦")
1
>>> next(z,"没啦")
2
>>> next(z,"没啦")
3
>>> next(z,"没啦")
4
>>> next(z,"没啦")
5
>>> next(z,"没啦")
'没啦'
>>> ["解决办法即是加入一个 默认值,即上述的 “没啦”,即可不会报错"]
['解决办法即是加入一个 默认值,即上述的 “没啦”,即可不会报错']
>>>

哥白尼 发表于 2021-12-7 20:54:42

小古比鱼 发表于 2021-10-25 11:27
本节继续讲解与序列相关的内置函数,讲了all()、any()、enumerate()、zip()、map()、filter()、iter()和nex ...

不好意思,我一直没理解迭代器是什么意思,可以讲解一下吗。

哥白尼 发表于 2021-12-7 20:55:40

这个迭代器是什么意思啊 一直没明白迭代器是啥意思。

小古比鱼 发表于 2021-12-8 09:13:37

哥白尼 发表于 2021-12-7 20:54
不好意思,我一直没理解迭代器是什么意思,可以讲解一下吗。

按照小古比鱼的理解,迭代器就是一个可迭代对象,即可以实现迭代功能的对象。至于何为可迭代对象,何为迭代,请参考新版教程第018讲以及旧版教程第016讲测试题第2题。
https://fishc.com.cn/thread-164088-1-1.html
https://fishc.com.cn/thread-41512-1-1.html

码农心 发表于 2022-5-10 22:00:46

难搞哟,忙了一天眼睛都是酸的

ctx111 发表于 2022-8-8 16:15:18

years=
enumerate(years)
<enumerate object at 0x10b9febc0>
list(enumerate(years))
[(0, 1997), (1, 1998), (2, 1999), (3, 2000)]
list(enumerate(years),100)
Traceback (most recent call last):
File "<pyshell#3>", line 1, in <module>
    list(enumerate(years),100)
TypeError: list expected at most 1 argument, got 2
list(enumerate(years),100))
SyntaxError: unmatched ')'
list((enumerate(years),100))
[<enumerate object at 0x10ab08600>, 100]
list((enumerate(years),100))
[<enumerate object at 0x10a90cdc0>, 100]
list(enumerate(years,100))
[(100, 1997), (101, 1998), (102, 1999), (103, 2000)]
list(enumerate(years))
[(0, 1997), (1, 1998), (2, 1999), (3, 2000)]
a=
b=
zipped=zip(a,b)
zipped
<zip object at 0x10bca5040>
list(zipped)
[(1, 4), (2, 5), (3, 6)]
c=
zipped=zip(a,b,c)
list(zipped)
[(1, 4, 7), (2, 5, 8), (3, 6, 9)]
z='ctxhs'
zipped=zip(a,b,z)
listed(zipped)
Traceback (most recent call last):
File "<pyshell#23>", line 1, in <module>
    listed(zipped)
NameError: name 'listed' is not defined. Did you mean: 'list'?
list(zipped)
[(1, 4, 'c'), (2, 5, 't'), (3, 6, 'x')]
import itertools
zipped=itertools._longest(a,b,z)
Traceback (most recent call last):
File "<pyshell#26>", line 1, in <module>
    zipped=itertools._longest(a,b,z)
AttributeError: module 'itertools' has no attribute '_longest'. Did you mean: 'zip_longest'?
zipped=itertools.zip_longest(a,b,z)
list(zipped)
[(1, 4, 'c'), (2, 5, 't'), (3, 6, 'x'), (None, None, 'h'), (None, None, 's')]
mapped=map(pow,,)
list(mapped)

mapped=(min,,)
Traceback (most recent call last):
File "<pyshell#31>", line 1, in <module>
    mapped=(min,,)
TypeError: list indices must be integers or slices, not tuple
mapped=(map(min,,))
Traceback (most recent call last):
File "<pyshell#32>", line 1, in <module>
    mapped=(map(min,,))
TypeError: list indices must be integers or slices, not tuple
list(map(min,,))
Traceback (most recent call last):
File "<pyshell#35>", line 1, in <module>
    list(map(min,,))
TypeError: list indices must be integers or slices, not tuple
list(map(min,,))
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
    list(map(min,,))
TypeError: list indices must be integers or slices, not tuple
list(map(min,,))
Traceback (most recent call last):
File "<pyshell#37>", line 1, in <module>
    list(map(min,,))
TypeError: list indices must be integers or slices, not tuple
list(map(max,,))
Traceback (most recent call last):
File "<pyshell#38>", line 1, in <module>
    list(map(max,,))
TypeError: list indices must be integers or slices, not tuple
list(map(max,,))
Traceback (most recent call last):
File "<pyshell#39>", line 1, in <module>
    list(map(max,,))
TypeError: list indices must be integers or slices, not tuple
>>> list(map(max, , , ))
SyntaxError: invalid syntax
list(map(max, , , ))

list(map(max,,))
Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
    list(map(max,,))
TypeError: list indices must be integers or slices, not tuple
list(map(max, , , ))

list(map(max,,))
Traceback (most recent call last):
File "<pyshell#44>", line 1, in <module>
    list(map(max,,))
TypeError: list indices must be integers or slices, not tuple
list(map(max, , , ))

list(map(min, , , ))

filter(str.isupper, "CtXHss")
<filter object at 0x10bcebaf0>
li(filter)
Traceback (most recent call last):
File "<pyshell#48>", line 1, in <module>
    li(filter)
NameError: name 'li' is not defined
list(filter)
Traceback (most recent call last):
File "<pyshell#49>", line 1, in <module>
    list(filter)
TypeError: 'type' object is not iterable
list(filter(str.isupper, "CtXHss"))
['C', 'X', 'H']
mapped=map(ord,'ctx')
for each in mapped:
    print(each)

   
99
116
120
list(mapped)
[]
x=
y=iter(x)
type(x)
<class 'list'>
type(y)
<class 'list_iterator'>
next(y)
1
next(y)
2
next(y)
3
next(y)
Traceback (most recent call last):
File "<pyshell#63>", line 1, in <module>
    next(y)
StopIteration
z=iter(x)
next(z,'被掏空了')
   
SyntaxError: invalid character ')' (U+FF09)
next(z,'被掏空了')
   
1
next(z,'被掏空了')
   
2
next(z,'被掏空了')
   
3
next(z,'被掏空了')
   
'被掏空了'

JZt2902019046 发表于 2022-8-14 20:23:13

打卡

元豪 发表于 2022-8-16 20:46:15

{:10_256:}

回家修别墅啊 发表于 2022-8-17 16:23:07

炎凉来寻 发表于 2022-9-2 17:18:43

打卡{:10_257:}

墨墨在努力吖 发表于 2022-10-7 17:58:58

滴滴滴~打卡{:10_298:}

migu_sm1 发表于 2022-11-4 15:28:59

Learning...

jgz1818 发表于 2022-11-23 22:58:58

迭代器和迭代对象还是有点檬{:10_266:}

xiaoxiangx 发表于 2023-1-11 21:36:13

打卡

Wynn_0303 发表于 2023-1-30 17:04:10

打卡

headfriend 发表于 2023-2-15 23:35:19

感觉晕乎乎的,{:5_107:}看了视频等于喝了半斤高度酒。

学爬虫的先生 发表于 2023-2-21 23:38:22

判断真假值
        all() 函数是判断可迭代对象中是否所有元素的值都为真;
        any() 函数则是判断可迭代对象中是否存在某个元素的值为真。
        >>> x =
>>> y =
>>> all(x)
False
>>> all(y)
Ture
>>> any(x)
True
>>> any(y)
True
枚举对象 ->将可迭代对象中的每个元素及从 0 开始的序号共同构成一个二元组的列表
        enumerate() 函数用于返回一个枚举对象,它的功能就是将可迭代对象中的每个元素及从 0 开始的序号共同构成一个二元组的列表:
                >>> seasons = ["Spring", "Summer", "Fall", "Winter"]
>>> list(enumerate(seasons))
[(0, 'Spring'), (1, 'Summer'), (2, 'Fall'), (3, 'Winter')]
                start 参数 自定义序号开始的值
                        >>> for i, j in enumerate(seasons, start=10):
...   print(i, "->", j)
...
10 -> Spring
11 -> Summer
12 -> Fall
13 -> Winter
zip() 创建一个聚合多个可迭代对象的迭代器
        做法是将作为参数传入的每个可迭代对象的每个元素依次组合成元组,即第 i 个元组包含来自每个参数的第 i 个元素。
                >>> x =
>>> y =
>>> zipped = zip(x, y)
>>> list(zipped)
[(1, 4), (2, 5), (3, 6)]
>>> z =
>>> 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() 函数 根据提供的函数对指定的可迭代对象的每个元素进行运算,并将返回运算结果的迭代器
        >>> mapped = map(ord, "FishC")
>>> list(mapped)

        指定的函数需要两个参数,可迭代对象的数量也应该是两个
                >>> mapped = map(pow, , ))
>>> list(mapped)

上面代码其实就相当于是:
>>>

        可迭代对象的长度不一致,那么 Python 采取的做法跟 zip() 函数一样,都是在最短的可迭代对象终止时结束
                >>> list(map(max, , , ))
##这里对比的是三个列表中每一个下标索引的最大值,而8这个长度其余没有,所以就不计入最大值
filter() 函数
        >>> filter(str.islower, "FishC")
<filter object at 0x000001B5170FEFA0>
        >>> list(filter(None, ))

可迭代对象和迭代器
        iter() 函数 将可迭代对象转换为迭代器
                >>> x =
>>> y = iter(x)
       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#52>", 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, "没啦,被你掏空啦~")
'没啦,被你掏空啦~'
页: [1] 2
查看完整版本: 第035讲:序列(下)