您的位置:首页 > 编程语言 > Python开发

python itertools

2013-07-10 22:46 344 查看
python itertools 是迭代器的标准包 以下是他的方法和用法

1、chain 将多个列表迭代,参数是列表

import itertools  

listone = ['a','b','c']  

listtwo = ['11','22','abc']  

for item in  itertools.chain(listone,listtwo):  

    print item

 

2、count 是一个无界的迭代

itertools.count(start,step)只能是数字。。。

3、cycle (列表) 只能是一个列表。。。。。

从列表中取元素,到列表尾后再从头取...

4、ifilter(fun,iterator)返回一个可以让fun返回True的迭代器,

5、imap(fun,iterator)

返回一个迭代器,对iterator中的每个项目调用fun

6.islice的使用

islice()(seq, [start,] stop [, step])

功能:返回迭代器,其中的项目来自 将seq,从start开始,到stop结束,以step步长切割后

7.izip的使用     izip(*iterator)

功能:返回迭代器,项目是元组,元组来自*iterator的组合

8. repeate  repeate(elem [,n])

功能:重复elem n次

官方帮助文件

    count(
) --> n, n+1, n+2, ...

    cycle(p) --> p0, p1, ... plast, p0, p1, ...

    repeat(elem [,n]) --> elem, elem, elem, ... endlessly or up to n times

    

    Iterators terminating on the shortest input sequence:

    chain(p, q, ...) --> p0, p1, ... plast, q0, q1, ... 

    compress(data, selectors) --> (d[0] if s[0]), (d[1] if s[1]), ...

    dropwhile(pred, seq) --> seq
, seq[n+1], starting when pred fails

    groupby(iterable[, keyfunc]) --> sub-iterators grouped by value of keyfunc(v)

    ifilter(pred, seq) --> elements of seq where pred(elem) is True

    ifilterfalse(pred, seq) --> elements of seq where pred(elem) is False

    islice(seq, [start,] stop [, step]) --> elements from

           seq[start:stop:step]

    imap(fun, p, q, ...) --> fun(p0, q0), fun(p1, q1), ...

    starmap(fun, seq) --> fun(*seq[0]), fun(*seq[1]), ...

    tee(it, n=2) --> (it1, it2 , ... itn) splits one iterator into n

    takewhile(pred, seq) --> seq[0], seq[1], until pred fails

    izip(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... 

    izip_longest(p, q, ...) --> (p[0], q[0]), (p[1], q[1]), ... 

    

    Combinatoric generators:

    product(p, q, ... [repeat=1]) --> cartesian product

    permutations(p[, r])

    combinations(p, r)

    combinations_with_replacement(p, r)
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  python itertools