温馨提示×

温馨提示×

您好,登录后才能下订单哦!

密码登录×
登录注册×
其他方式登录
点击 登录注册 即表示同意《亿速云用户服务条款》

Python iter()的用法有哪些

发布时间:2021-11-20 16:30:36 来源:亿速云 阅读:136 作者:iii 栏目:编程语言

本篇内容主要讲解“Python iter()的用法有哪些”,感兴趣的朋友不妨来看看。本文介绍的方法操作简单快捷,实用性强。下面就让小编来带大家学习“Python iter()的用法有哪些”吧!

一、上代码、学用法

我们都比较熟悉 iter(obj),会返现一个迭代器,如果 obj 不是可迭代对象,则会报错。但其实如果仔细看官方文档,会发现 iter() 方法其实是接受两个参数的,文档说明如下

iter(object[, sentinel])

sentinel 英文翻译为 哨兵。

sentinel 参数是可选的,当它存在时,object 不再传入一个可迭代对象,而是一个可调用对象,通俗点说就是可以通过()调用的对象,而 sentinel 的作用就和它的翻译一样,是一个“哨兵”,当时可调用对象返回值为这个“哨兵”时,循环结束,且不会输出这个“哨兵”。

可能有点难懂,用一个简单需求来说明,需求说明如下:

心里想一个[1, 10]范围的数,然后代码开始随机,当随机到想的数时停止,看每次代码需要随机几次。

实现分析:看起来应该很简单,random,然后加一个if判断即可,但是用 iter() 来实现更简单。实现代码如下:

from random import randint
def guess():
 return randint(0, 10)
num = 1
# 这里先写死心里想的数为5
for i in iter(guess, 5):
 print("第%s次猜测,猜测数字为: %s" % (num, i))
 num += 1
# 当 guess 返回的是 5 时,会抛出异常 StopIteration,但 for 循环会处理异常,即会结束循环

二、还是看看文档吧

关于这两个参数,文档里也说的很详细,分段解释如下:

The first argument is interpreted very differently depending on the presence of the second argument.

翻译:第一个参数根据第二个参数有不同的含义

Without a second argument, object must be a collection object which supports the iteration protocol (the _iter_() method), or it must support the sequence protocol (the _getitem_() method with integer arguments starting at 0). If it does not support either of those protocols, TypeError is raised.

翻译:如果没有第二个参数,object(即第一个参数)是一个支持迭代器协议(实现_iter_()方法的)的集合对象,或者是支持序列协议(实现_getitem_()方法)且是从0开始索引。如果它不支持其中任何一个,则抛出 TypeError 异常

简单来说就是,如果没有第二个参数,就是我们比较熟悉的用法。代码示例如下:

In [5]: iter("123")
Out[5]: <str_iterator at 0x105c9b9e8>
In [6]: iter([1, 2, 3])
Out[6]: <list_iterator at 0x105f9f8d0>
In [7]: iter(123)
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-7-c76acad08c3c> in <module>()
----> 1 iter(123)
TypeError: 'int' object is not iterable

再来看看有第二个参数的情况

If the second argument, sentinel, is given, then object must be a callable object. The iterator created in this case will call object with no arguments for each call to its _next_() method; if the value returned is equal to sentinel, StopIteration will be raised, otherwise the value will be returned.

翻译:如果给定了第二个参数 sentinel,object 则必须是一个可调用对象,这个可调用对象没有任何参数,当可调用对象的返回值等于 sentinel 的值时,抛出 StopIteration 的异常,否则返回当前值。(这里如果不好理解可调用对象,可以理解为函数,这样更容易想明白)

对于这个用法的适用场景,文档中也给出了说明:

One useful application of the second form of iter() is to build a block-reader. For example, reading fixed-width blocks from a binary database file until the end of file is reached:

翻译:对于第二个参数,一个有用的场景是创建一个 blokc-reader,即根据条件中断读取。比如:从二进制数据库文件读取固定宽度的块,直到到达文件的末尾,代码示例如下:

from functools import partial
with open('mydata.db', 'rb') as f:
 for block in iter(partial(f.read, 64), b''):
 process_block(block)

到此,相信大家对“Python iter()的用法有哪些”有了更深的了解,不妨来实际操作一番吧!这里是亿速云网站,更多相关内容可以进入相关频道进行查询,关注我们,继续学习!

向AI问一下细节

免责声明:本站发布的内容(图片、视频和文字)以原创、转载和分享为主,文章观点不代表本网站立场,如果涉及侵权请联系站长邮箱:is@yisu.com进行举报,并提供相关证据,一经查实,将立刻删除涉嫌侵权内容。

AI