python iter( )函数

2024-03-03 14:08
文章标签 python 函数 iter

本文主要是介绍python iter( )函数,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

python中的迭代器用起来非常灵巧,不仅可以迭代序列,也可以迭代表现出序列行为的对象,例如字典的键、一个文件的行,等等。

迭代器就是有一个next()方法的对象,而不是通过索引来计数。当使用一个循环机制需要下一个项时,调用迭代器的next()方法,迭代完后引发一个StopIteration异常。
但是迭代器只能向后移动、不能回到开始、再次迭代只能创建另一个新的迭代对象。
反序迭代工具:reversed()将返回一个反序访问的迭代器。python中提供的迭代模块:itertools模块

先看几个例子:

>>> l=[2,3,4]
>>> iterl=iter(l)
>>> iterl.next()
2
>>> iterl.next()
3
>>> iterl.next()
4
>>> iterl.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


>>> d={'one':1,'two':2,'three':3}
>>> d
{'three': 3, 'two': 2, 'one': 1}
>>> iterd=iter(d) #字典的迭代器会遍历字典的键(key)
>>> iterd.next()
'three'
>>> iterd.next()
'two'
>>> iterd.next()
'one'
>>> iterd.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


下面查看iter()函数的帮助信息:

>>> help(iter)

Help on built-in function iter in module __builtin__:


iter(...)
    iter(collection) -> iterator
    iter(callable, sentinel) -> iterator
    
    Get an iterator from an object.  In the first form, the argument must
    supply its own iterator, or be a sequence.

    In the second form, the callable is called until it returns the sentinel.

iter()函数有两种用法,一种是传一个参数,一种是传两个参数。结果都是返回一个iterator对象。

所谓的iterator对象,就是有个next()方法的对象。next方法的惯例或约定(convention)是,每执行一次就返回下一个值(因此它要自己记录状态,通常是在iterator对象上记录),直到没有值的时候raiseStopIteration。

传1个参数:参数collection应是一个容器,支持迭代协议(即定义有__iter__()函数),或者支持序列访问协议(即定义有__getitem__()函数),否则会返回TypeError异常。

传2个参数:当第二个参数sentinel出现时,参数callable应是一个可调用对象(实例),即定义了__call__()方法,当枚举到的值等于哨兵时,就会抛出异常StopIteration。

>>> s='abc'  #s支持序列访问协议,它有__getitem__()方法 

>>> help(str.__getitem__)
Help on wrapper_descriptor:


__getitem__(...)
    x.__getitem__(y) <==> x[y]

>>> s.__getitem__(1)
'b'
>>> s[1]
'b'


>>> iters=iter(s) #iters是一个iterator对象,它有next()和__iter__()方法
>>> iters1=iters.__iter__()
>>> iters2=iter(iters)
>>> iters
<iterator object at 0x030612D0>
>>> iters1
<iterator object at 0x030612D0>
>>> iters2
<iterator object at 0x030612D0>

iters  iters1   iters2 是同一个迭代器!!
>>> iters.next()
'a'
>>> iters.next()
'b'
>>> iters.next()
'c'
>>> iters.next()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
StopIteration


>>> class test:  # test 类支持迭代协议,因为它定义有__iter__()函数
...     def __iter__(self):
...         print '__iter__ is called!'
...         self.result=[1,2,3]
...         return iter(self.result)
... 
>>> t=test()  # t支持迭代协议

>>> for i in t:   #当执行for i in t 时,实际上是调用了t.__iter__(),也就是__iter__(t),返回一个iterator对象
...     print i,
... 
__iter__ is called!
1 2 3

>>> for i in t.__iter__():
                print i,

__iter__ is called!!
1 2 3
>>> for i in test.__iter__(t):
                print i,

__iter__ is called!!
1 2 3


>>> l=[1,2,3]
>>> for i in l:
...     print i,
... 
1 2 3

#上述for循环实际上是这样工作的(for循环会自动调用迭代器的next()方法),如下:

>>> iterl=iter(l)
>>> while True:
...     try:
...         i=iterl.next()
...     except StopIteration:
...         break
...     print i,
... 
1 2 3



>>> f=open(r'C:\Users\Administrator\Desktop\test.txt','w')
>>> f.writelines(['love python\n','hello python\n','love python\n'])
>>> f.close()
>>> f=open(r'C:\Users\Administrator\Desktop\test.txt','r')
>>> for line in f:  # 文件对象生成的迭代器会自动调用readline()方法,这样循环遍历就可以访问文本文件的所有行
...     print line[:-1]
... 
love python
hello python
love python

上述for循环部分功能与以下代码一致:

>>> while True:
...     line=f.readline()
...     if line!='':
...         print line[:-1]
...     else:
...         break
... 
love python
hello python
love python


>>> f=open(r'C:\Users\91135\Desktop\test.txt','r')
>>> f.readlines()
['love python\n', 'hello python\n', '\n', 'love python\n']
>>> f.seek(0)
>>> f.next()
'love python\n'
>>> f.next()
'hello python\n'
>>> f.next()
'\n'
>>> f.next()
'love python\n'
>>> f.next()
Traceback (most recent call last):
  File "<pyshell#140>", line 1, in <module>
    f.next()
StopIteration
>>> f.seek(0)
>>> it1=iter(f)
>>> it2=f.__iter__()

f    iter1    iter2 三者是同一个对象!!!
>>> f
<open file 'C:\\Users\\91135\\Desktop\\test.txt', mode 'r' at 0x030E9A70>
>>> it1
<open file 'C:\\Users\\91135\\Desktop\\test.txt', mode 'r' at 0x030E9A70>
>>> it2
<open file 'C:\\Users\\91135\\Desktop\\test.txt', mode 'r' at 0x030E9A70>
>>> f.next()
'love python\n'
>>> it1.next()
'hello python\n'
>>> next(it2)
'\n'
>>> next(f)
'love python\n'
>>> next(f)
Traceback (most recent call last):
  File "<pyshell#247>", line 1, in <module>
    next(f)
StopIteration
>>> it1.next()
Traceback (most recent call last):
  File "<pyshell#248>", line 1, in <module>
    it1.next()
StopIteration
>>> it2.next()
Traceback (most recent call last):
  File "<pyshell#249>", line 1, in <module>
    it2.next()
StopIteration


iter(callable, sentinel) -> iterator
如果是传递两个参数给 iter() , 第一个参数必须是callable ,它会重复地调用第一个参数, 
直到迭代器的下个值等于sentinel:即在之后的迭代之中,迭代出来sentinel就立马停止。
关于Python中,啥是可调用的,可以参考:python callable()函数
>>> class IT(object):
        def __init__(self):
               self.l=[1,2,3,4,5]
               self.i=iter(self.l)
        def __call__(self):   #定义了__call__方法的类的实例是可调用的
               item=next(self.i)
               print "__call__ is called,which would return",item
               return item
        def __iter__(self): #支持迭代协议(即定义有__iter__()函数)
               print "__iter__ is called!!"
               return iter(self.l)

>>> it=IT() #it是可调用的
>>> it1=iter(it,3) #it必须是callable的,否则无法返回callable_iterator
>>> callable(it)
True
>>> it1
<callable-iterator object at 0x0306DD90>
>>> for i in it1:
print i

__call__ is called,which would return 1
1
__call__ is called,which would return 2
2
__call__ is called,which would return 3

可以看到传入两个参数得到的it1的类型是一个callable_iterator,它每次在调用的时候,都会调用__call__函数,并且最后输出3就停止了。
>>> it2=iter(it)
__iter__ is called!!
>>> it2
<listiterator object at 0x030A1FD0>
>>> for i in it2:
print i,


1 2 3 4 5
 
与it1相比,it2就简单的多,it把自己类中一个容器的迭代器返回就可以了。 

上面的例子只是为了介绍iter()函数传两个参数的功能而写,如果真正想写一个iterator的类,还需要定义next函数,这个函数每次返回一个值就可以实现迭代了。
>>> class Next():
                def __init__(self,data=825):
                           self.data=data
                def __iter__(self):
                           return self
               def next(self):
                           print "next is called!!"
                           if self.data>828:
                                   raise StopIteration
                           else:
                                   self.data+=1
                                   return self.data

>>> for i in Next():
print i


next is called!!
826
next is called!!
827
next is called!!
828
next is called!!
829
next is called!!
>>> for i in Next(826):
print i


next is called!!
827
next is called!!
828
next is called!!
829
next is called!!
>>> 
唯一需要注意下的就是next中必须控制iterator的结束条件,不然就死循环了。

>>> it=Next()
>>> it.__iter__()
<__main__.Next instance at 0x02E75F80>
>>> Next.__iter__(it)
<__main__.Next instance at 0x02E75F80>
>>> iter(it)
<__main__.Next instance at 0x02E75F80>
>>> it
<__main__.Next instance at 0x02E75F80>


>>> it=Next()
>>> it.next()
next is called!!
826
>>> next(it)
next is called!!
827
>>> Next.next(it)
next is called!!
828
>>> next(it)
next is called!!
829
>>> it.next()
next is called!!

Traceback (most recent call last):
  File "<pyshell#68>", line 1, in <module>
    it.next()
  File "<pyshell#1>", line 9, in next
    raise StopIteration
StopIteration

(完)


这篇关于python iter( )函数的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/769842

相关文章

Python开发文字版随机事件游戏的项目实例

《Python开发文字版随机事件游戏的项目实例》随机事件游戏是一种通过生成不可预测的事件来增强游戏体验的类型,在这篇博文中,我们将使用Python开发一款文字版随机事件游戏,通过这个项目,读者不仅能够... 目录项目概述2.1 游戏概念2.2 游戏特色2.3 目标玩家群体技术选择与环境准备3.1 开发环境3

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

一文教你Python如何快速精准抓取网页数据

《一文教你Python如何快速精准抓取网页数据》这篇文章主要为大家详细介绍了如何利用Python实现快速精准抓取网页数据,文中的示例代码简洁易懂,具有一定的借鉴价值,有需要的小伙伴可以了解下... 目录1. 准备工作2. 基础爬虫实现3. 高级功能扩展3.1 抓取文章详情3.2 保存数据到文件4. 完整示例

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

基于Python打造一个智能单词管理神器

《基于Python打造一个智能单词管理神器》这篇文章主要为大家详细介绍了如何使用Python打造一个智能单词管理神器,从查询到导出的一站式解决,感兴趣的小伙伴可以跟随小编一起学习一下... 目录1. 项目概述:为什么需要这个工具2. 环境搭建与快速入门2.1 环境要求2.2 首次运行配置3. 核心功能使用指

Python实现微信自动锁定工具

《Python实现微信自动锁定工具》在数字化办公时代,微信已成为职场沟通的重要工具,但临时离开时忘记锁屏可能导致敏感信息泄露,下面我们就来看看如何使用Python打造一个微信自动锁定工具吧... 目录引言:当微信隐私遇到自动化守护效果展示核心功能全景图技术亮点深度解析1. 无操作检测引擎2. 微信路径智能获

Python中pywin32 常用窗口操作的实现

《Python中pywin32常用窗口操作的实现》本文主要介绍了Python中pywin32常用窗口操作的实现,pywin32主要的作用是供Python开发者快速调用WindowsAPI的一个... 目录获取窗口句柄获取最前端窗口句柄获取指定坐标处的窗口根据窗口的完整标题匹配获取句柄根据窗口的类别匹配获取句

利用Python打造一个Excel记账模板

《利用Python打造一个Excel记账模板》这篇文章主要为大家详细介绍了如何使用Python打造一个超实用的Excel记账模板,可以帮助大家高效管理财务,迈向财富自由之路,感兴趣的小伙伴快跟随小编一... 目录设置预算百分比超支标红预警记账模板功能介绍基础记账预算管理可视化分析摸鱼时间理财法碎片时间利用财