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()和它相反。

分组

没看明白。

原文地址:https://www.cnblogs.com/lexus/p/1844609.html