python itertools模块学习

一直对itertools怀有敬畏之心,其实会了就还好了,that's nothing

 

这里的迭代器最主要得特点就是lazy,和stl里面得迭代器还是不一样得,最明显得好处就是节省内存了。

顾名思义,itertools得函数返回得都是迭代器,为简单起事,下面就不专门说明了

count(p,q) 返回p, p+q, p+2*q, ....

cycle(p)  返回 p[0], p[1],...p[last],p[0],p[1]......

repeat(p, n) 返回 p...p (n times)

chain(p,q..) 把几个参数连接起来

compress(data, selectors)   比如compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F   

dropwhile(pred, seq)    比如dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1

 

In [101]: for n, v in itertools.groupby('aaabbbb ccc aaaaa'):  
.....: print n, list(v)
.....:
a ['a', 'a', 'a']
b ['b', 'b', 'b', 'b']
['']
c ['c', 'c', 'c']
['']
a ['a', 'a', 'a', 'a', 'a']



ifilter(pred, seq)      ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9

ifilterfalse(pred, seq)  ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8

islice(seq, start, stop, step)     islice('ABCDEFG', 2, None) --> C D E F G

imap(fun, p, q)                    imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000

starmap(fun, seq)                  starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000

tee(it, n)  将迭代器it复制n份

takewhile(pred, seq)               takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4

izip(p,q,...)                      izip('ABCD', 'xy') --> Ax By

izip_longest(p,q,...)              izip_longest('ABCD', 'xy', fillvalue='-') --> Ax ByC- D-

product(p,q,...)   笛卡尔积

permutations(seq, r)              permutations('ABCD', 2) -->AB AC AD BA BC BD CA CB CD DA DB DC 

combination(seq, r)                          combinations('ABCD', 2) -->AB AC AD BC BD CD

combinations_with_replacement(seq, r)    combinations_with_replacement('ABCD', 2) -->AA AB AC AD BB BC BD CC CD DD

 

其实这些都是python document里面得内容了,一直没细看,今天终于抽空看了下

posted @ 2011-10-25 22:09  酱油哥  阅读(193)  评论(0编辑  收藏  举报