英文文档:

iter(object[, sentinel])

Return an iterator object. 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. 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 sentinelStopIteration will be raised, otherwise the value will be returned.

One useful application of the second form of iter() is to read lines of a file until a certain line is reached. The following example reads a file until the readline() method returns an empty string:

with open('mydata.txt') as fp:
for line in iter(fp.readline, ''):
process_line(line)

  根据传入的参数生成一个新的可迭代对象

说明:

  1. 函数功能返回一个迭代器对象。

  2. 当第二个参数不提供时,第一个参数必须是一个支持可迭代协议(即实现了__iter__()方法)的集合(字典、集合、不可变集合),或者支持序列协议(即实现了__getitem__()方法,方法接收一个从0开始的整数参数)的序列(元组、列表、字符串),否则将报错。

>>> a = iter({'A':1,'B':2}) #字典集合
>>> a
<dict_keyiterator object at 0x03FB8A50>
>>> next(a)
'A'
>>> next(a)
'B'
>>> next(a)
Traceback (most recent call last):
File "<pyshell#36>", line 1, in <module>
next(a)
StopIteration >>> a = iter('abcd') #字符串序列
>>> a
<str_iterator object at 0x03FB4FB0>
>>> next(a)
'a'
>>> next(a)
'b'
>>> next(a)
'c'
>>> next(a)
'd'
>>> next(a)
Traceback (most recent call last):
File "<pyshell#29>", line 1, in <module>
next(a)
StopIteration

  3. 当第二个参数sentinel提供时,第一个参数必须是一个可被调用对象。创建的迭代对象,在调用__next__方法的时候会调用这个可被调用对象,当返回值和sentinel值相等时,将抛出StopIteration异常, 终止迭代。

# 定义类
>>> class IterTest:
def __init__(self):
self.start = 0
self.end = 10
def get_next_value(self):
current = self.start
if current < self.end:
self.start += 1
else:
raise StopIteration
return current >>> iterTest = IterTest() #实例化类
>>> a = iter(iterTest.get_next_value,4) # iterTest.get_next_value为可调用对象,sentinel值为4
>>> a
<callable_iterator object at 0x03078D30>
>>> next(a)
0
>>> next(a)
1
>>> next(a)
2
>>> next(a)
3
>>> next(a) #迭代到4终止
Traceback (most recent call last):
File "<pyshell#22>", line 1, in <module>
next(a)
StopIteration

最新文章

  1. flask学习资源
  2. How to evaluate a transimpedance amplifier (part 1)
  3. POJ C程序设计进阶 编程题#3: 发票统计
  4. python三元运算符
  5. linux内核源码阅读之facebook硬盘加速利器flashcache
  6. java.lang.Math类,方法学习笔记
  7. 教你如何取消GCD任务
  8. 8.5 filecmp--文件和文件夹比較处理
  9. BZOJ 1935: [Shoi2007]Tree 园丁的烦恼 [树状数组 离线 离散化]
  10. js 原型,原型链,原型链继承浅析
  11. codeforces#1152C. Neko does Maths(最小公倍数)
  12. 爬虫 requests模块的其他用法 抽屉网线程池回调爬取+保存实例,gihub登陆实例
  13. 关于总结一些CentOS7常用的运维命令
  14. keepliave
  15. encoding and Endian
  16. 使用Pabot并行运行RF案例
  17. webservice客户端 get delete post 请求
  18. html之div始终停留在屏幕中间部分
  19. VMware 怎么判断哪台机子试图用混杂模式且不成功
  20. CentOS安装Oracle 11g R2

热门文章

  1. 大三小学期 web前端开发的一些小经验
  2. 读 《 Web 研发模式的演变 》与《Javascript:世纪机器语言》
  3. Xamarin改变移动开发的五个理由
  4. Problem : 1013 ( Digital Roots )
  5. 使用MBROSTool 工具制作本地硬盘多启动盘的方法总结
  6. 设计模式——命令模式(C++实现)
  7. Opencv读取并获取视频属性
  8. 基于synchronized实现的阻塞队列
  9. 阿里图标库iconfont入门使用
  10. 利用whoosh对mongoDB的中文文档建立全文检索