Python内置函数(36)——iter
英文文档:
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 sentinel, StopIteration
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
Python内置函数(36)——iter的更多相关文章
- Python内置函数(28)——iter
英文文档: iter(object[, sentinel]) Return an iterator object. The first argument is interpreted very dif ...
- Python内置函数(36)——reversed
英文文档: reversed(seq) Return a reverse iterator. seq must be an object which has a __reversed__() meth ...
- Python内置函数(4)
Python内置函数(4) 1.copyright 交互式提示对象打印许可文本,一个列表贡献者和版权声明 2.credits 交互式提示对象打印许可文本,一个贡献者和版权声明的列表 3.delattr ...
- Python内置函数和内置常量
Python内置函数 1.abs(x) 返回一个数的绝对值.实参可以是整数或浮点数.如果实参是一个复数,返回它的模. 2.all(iterable) 如果 iterable 的所有元素为真(或迭代器为 ...
- Python | 内置函数(BIF)
Python内置函数 | V3.9.1 | 共计155个 还没学完, 还没记录完, 不知道自己能不能坚持记录下去 1.ArithmeticError 2.AssertionError 3.Attrib ...
- Python 内置函数笔记
其中有几个方法没怎么用过, 所以没整理到 Python内置函数 abs(a) 返回a的绝对值.该参数可以是整数或浮点数.如果参数是一个复数,则返回其大小 all(a) 如果元组.列表里面的所有元素都非 ...
- 【转】python 内置函数总结(大部分)
[转]python 内置函数总结(大部分) python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为 ...
- python内置函数,匿名函数
一.匿名函数 匿名函数:为了解决那些功能很简单的需求而设计的一句话函数 def calc(n): return n**n print(calc(10)) #换成匿名函数 calc = lambda n ...
- python 内置函数总结(大部分)
python 内置函数大讲堂 python全栈开发,内置函数 1. 内置函数 python的内置函数截止到python版本3.6.2,现在python一共为我们提供了68个内置函数.它们就是pytho ...
随机推荐
- Visual Studio Code 搭建Python开发环境
1.下载Python https://www.python.org/downloads/windows/ 选择一个版本,目前2.0的源码比较多,我下载的2.7.12 2.配置环境变量 3.Visual ...
- *CTF——shellcode
一看题目是利用shellcode解决问题 伪代码: checksec:开启了NX exp: from pwn import* context(os='linux',arch='amd64',log ...
- 安装jar包到maven仓库
1)将所要安装的jar包放在自定义目录下. 2)(maven环境变量配置无误的情况下)windows环境下,打开命令提示符,输入如下命令: mvn install:install-file -Dfil ...
- 在同一个Apache服务器软件上部署多个站点的基础方法
这篇文章主要介绍了Apache中Virtual Host虚拟主机配置及rewrite模块中的重要参数说明,是在同一个Apache服务器软件上部署多个站点的基础方法,需要的朋友可以参考下(http:// ...
- ImCash:论拥有靠谱数字钱包的重要性!
数字货币被盗已经不是什么新鲜事,前有交易所币安被黑客攻击,Youbit破产,后有“钓鱼邮件“盗号木马,安全对于数字货币用户来讲至关重要. 现行市场痛点: 2017年9月以太坊Parity钱包的漏洞 ...
- 第三次作业-结对编程(wordcount)
GIT地址 https://github.com/gentlemanzq/WordCount.git GIT用户名 gentlemanzq 结对伙伴博客地址 https://home.cnblogs ...
- Android 博客导航
Android 博客导航 一. 基础知识 Android 常用知识点 Android 常见问题解决 Android 常用控件 控件常用属性 Material Design 常用控件 二.常用知识点 动 ...
- BZOJ.2054.疯狂的馒头(并查集)
BZOJ 倒序处理,就是并查集傻题了.. 并查集就是确定下一个未染色位置的,直接跳到那个位置染.然而我越想越麻烦=-= 以为有线性的做法,发现还是要并查集.. 数据随机线段树也能过去. //18400 ...
- 教你如何用勾勾街快速生成一个苹果IOS APP
现在苹果手机上的各种各样的APP,想不想也有一款属于自己的专属APP?很简单,用勾勾街可以在3分钟内快速制作一款,快来看看! 工具平台: 勾勾街 (www.gogojie.com ) 操作方法: 1. ...
- 把ssl模块加入到已经编译好的apache中实现HTTPS
为了使Apache支持https访问,系统需要安有apache.openssl.mod_ssl.so 1.安装openssl: 基本上系统都已经安装了,在/usr/bin/openssl下,直接使用o ...