python迭代器
未理解
http://www.ibm.com/developerworks/cn/linux/l-cpyiter/index.html
Python标准库参考笔记- itertools 收藏
Python 标准库参考笔记 - itertools 10.7 itertools
地址: http://docs.python.org/library/itertools.html
PyMOTW :
http://www.doughellmann.com/PyMOTW/itertools/index.html
看官方文档看得一头雾水,直接看的 PyMOTW 。这篇笔记基本上是 PyMOTW 的翻译。
合并和分离 iterators
chain
使用 chain 将多个 iterators 合并成一个 itertator 。
# -*- coding: cp936 -*-
from itertools import *
for i in chain([1, 2, 3], ['a', 'b', 'c']):
print i
-------------------------------------------------------------------------------------------------------
结果:
>>>
1
2
3
a
b
c
izip
izip() 将多个 iterators 迭代的内容混合成一个元组,并返回这个元组的迭代器,和 zip() 类似。
# -*- coding: cp936 -*-
from itertools import *
for i in izip([1, 2, 3], ['a', 'b', 'c']):
print i
-------------------------------------------------------------------------------------------------------
>>>
(1, 'a')
(2, 'b')
(3, 'c')
islice
看 PyMOTW 的例子之前不了解 count 是干啥的, help 一下发现也是 itertools module 里的,作用就是从一个数 ( 默认为零 ) 开始迭代。
islice 接受一个 iterator 作为输入,返回指定的条目。和 slice 类似,其参数格式为 ( 起始索引 , 结束索引 , 步伐 ) 。例如:
# -*- coding: cp936 -*-
from itertools import *
print 'By tens to 100:'
for i in islice(count(), 0, 100, 10):
print i
-------------------------------------------------------------------------------------------------------
>>>
By tens to 100:
0
10
20
30
40
50
60
70
80
90
tee
接受一个 iterator ,返回多个 ( 两个 ) 与输入 iterator 相同的 iterators 。例如:
# -*- coding: cp936 -*-
from itertools import *
r = islice(count(), 5)
i1, i2 = tee(r)
for i in i1:
print 'i1:', i
for i in i2:
print 'i2:', i
-------------------------------------------------------------------------------------------------------
>>>
i1: 0
i1: 1
i1: 2
i1: 3
i1: 4
i2: 0
i2: 1
i2: 2
i2: 3
i2: 4
在对一个 iterator 调用了 tee 以后,对这个 iterator 的迭代会影响到 tee 所产生的那些 iterators 。
转换输入
imap
imap 接受一个函数,一个或者多个 iterators 作为输入,并将函数应用到各个 iterators 引用的元素上,返回结果。
# -*- coding: cp936 -*-
from itertools import *
# 一个 iterator
print 'Doubles:'
for i in imap(lambda x:2*x, xrange(5)):
print i
# 多个 iterators
print 'Multiples:'
for i in imap(lambda x,y:(x, y, x*y), xrange(5), xrange(5,10)):
print '%d * %d = %d' % i
-------------------------------------------------------------------------------------------------------
>>>
Doubles:
0
2
4
6
8
Multiples:
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
starmap
starmap() 函数和 imap 类似,不同之处在于只接受一个 iterator 作为参数,并且使用函数调用时候的 * 语法将 iterator 迭代的内容展开。假设传入的函数为 f 、迭代器为 i ,则相当于调用 f(*i) 。
# -*- coding: cp936 -*-
from itertools import *
values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]
for i in starmap(lambda x,y:(x, y, x*y), values):
print '%d * %d = %d' % i
-------------------------------------------------------------------------------------------------------
>>>
0 * 5 = 0
1 * 6 = 6
2 * 7 = 14
3 * 8 = 24
4 * 9 = 36
产生新值
count() 不断地产生整数, cycle() 不断地循环几个给定的输入, repeat() 不断地重复一个输入,它们的返回值都是 iterator 。
过滤
dropwhile() 接受一个测试函数和一个 iterator 作为输入,对 iterator 迭代的内容应用测试函数,当测试函数第一次返回 True 之后,迭代器开始返回其后的内容。 takewhile() 和 dropwhile() 相反。
ifilter 会对每一个元素应用测试函数,为 True 的就返回迭代的内容。 ifilterfalse() 和它相反。
分组
没看明白。