深入浅出 Python Iterators 迭代器
来源:互联网 发布:淘宝九块九在哪里 编辑:程序博客网 时间:2024/06/05 05:55
原文
- iterator protocol
- iterable
- iterator iter next
- for loop的实现原理
- 构建自己的Iterator
- Infinite Iterators
iterator protocol
iterator object必须实现两个method: __iter()__
和 __next()__
注意: Python 3 中使用
__iter__()
和__next__()
;Python 2 中使用__iter__()
和next()
iterable
iterable是个名词
list, tuple, dict都是iterable
可以用for x in yyy
形式的yyy
都是一个iterable
iterator、 iter()、 next()
如果一个对象yyy是iterable,那么可以用a = iter(yyy)
获得一个iterator a
也可以用
yyy.__iter__()
再用next(a)
手动进行一次迭代/iterate
也可以用
a.__next__()
当一个iterator已经迭代到头了,下一次next()
会报StopIteration
错误
下面是一个例子
# define a listmy_list = [4, 7, 0, 3]# get an iterator using iter()my_iter = iter(my_list)## iterate through it using next() #prints 4print(next(my_iter))#prints 7print(next(my_iter))## next(obj) is same as obj.__next__()#prints 0print(my_iter.__next__())#prints 3print(my_iter.__next__())## This will raise error, no items leftnext(my_iter)
for loop的实现原理
for element in iterable: # do something with element
对应的实现代码是:
# create an iterator object from that iterableiter_obj = iter(iterable)# infinite loopwhile True: try: # get the next item element = next(iter_obj) # do something with element except StopIteration: # if StopIteration is raised, break from loop break
构建自己的Iterator
对于一个类,只要实现__iter__()
和__next__()
方法即成为一个iterator __iter__()
method返回iterator对象本身,同时也可以做一些初始化工作 __next__()
method返回一系列迭代值,最后通过raise StopIteration错误来结束迭代
下面的例子每次迭代返回一个2幂次方,幂指数从0一直到用户给定的值
class PowTwo: """Class to implement an iterator of powers of two""" def __init__(self, max = 0): self.max = max def __iter__(self): self.n = 0 return self def __next__(self): if self.n <= self.max: result = 2 ** self.n self.n += 1 return result else: raise StopIteration
那么运行结果为:
>>> for i in PowTwo(5):... print(i)... 12481632
Infinite Iterators
迭代器可以是无限迭代的,例如依次返回所有的奇数。
不过使用时必须谨慎处理,加上适当的终止条件。
Python built-in 函数iter()
可以有两个参数
一参是一个callable object (如function)
二参是一个sentinel value (终止条件/ terminating condition)。
迭代器会调用这个function直到返回值等于sentinel value。
例如,
>>> int()0>>> inf = iter(int,1)>>> next(inf)0>>> next(inf)0
iter(int, 1)
返回了一个迭代器,这个迭代器遇到1时终止迭代。
注意:遇到1直接raise StopIteration,而不是下次迭代raise StopIteration
int()
(int的构造函数)永远都返回0
因此这个迭永远不会停止,即成为一个infinite iterator.
- 深入浅出 Python Iterators 迭代器
- 迭代器(Iterators)
- 迭代器 Iterators
- 迭代器(Iterators)
- C++ 迭代器(Iterators)
- Iterators (迭代器)
- c++迭代器iterators
- rust Iterators迭代器
- Iterators
- Iterators
- iterators
- python 高级迭代器 (http://sebug.net/paper/books/dive-into-python3/advanced-iterators.html)
- 【Python那些事儿之七】Iterators详解
- Python标准模块--Iterators和Generators
- Python标准模块--Iterators和Generators
- 失效迭代器(Invalidating Iterators)
- Reverse Iterators(逆向迭代器)
- 迭代器vector iterators incompatible错误;
- 【Git】git status 中文文件名编码问题解决
- 第6章 函数
- java面试总结-5
- java中equals与hashcode的方法使用
- 【设计模式】--TEMPLATE METHOD 模式 和 STRATEGY 模式 :继承和委托
- 深入浅出 Python Iterators 迭代器
- java.lang.ClassNotFoundException: org.springframework.web.servlet.DispatcherServlet
- Wannafly模拟赛5 A Split
- PCA
- Qt中的线程池
- ISE学习
- java IO 使用Properties设置键值对配置信息 写入文件和从文件中读取
- ngxin
- java 实现扑克牌洗牌功能