python模块之itertools

简介

NAME
    itertools - Functional tools for creating and using iterators.

FILE
    (built-in)

DESCRIPTION
    Infinite iterators:
    count([n]) --> 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[n], 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)

itertools. count

count(start=0, step=1) –> count object
Return a count object whose .next() method returns consecutive values.
在官方文档里面,有一段代码,解释说,count函数等价与下面的函数
Equivalent to:

def count(firstval=0, step=1):
    x = firstval
    while 1:
        yield x
        x += step

因此,count函数两个参数开始值和步长均有默认值,其返回一无界的迭代器,在循环里面调用的时候,要设置break条件。

#!/usr/bin/env python
#coding:utf8

""" Author: Description: learning itertools.count """

from  itertools import count
#使用默认参数,start = 0, step = 1
for item in count():
    print item,
    if item > 5:
        break

print
print "-"*20
#指定参数,start = 10, step = 2
for item in count(start = 10, step = 2):
    print item,
    if item > 20:
        break

#输出结果为:
0 1 2 3 4 5 6
--------------------
10 12 14 16 18 20 22

itertools.cycle

cycle(iterable) –> cycle object
Return elements from the iterable until it is exhausted.Then repeat the sequence indefinitely.
一个列表为例,返回每一个元素,直到列表的最后一个元素,然后重复返回这个列表,无限循环下去, 也是返回一个无界迭代器
直接上代码吧:

#!/usr/bin/env python
#coding:utf8

""" Author: Description: learning itertools.cycle """

from  itertools import cycle

l = ['a', 'b', 'c']
i = 0
for item in cycle(l):
    print item,
    i += 1
    if i > 4:
        break

#输出结果为:
a b c a b

itertools.repeat

repeat(object [,times]) -> create an iterator which returns the object
for the specified number of times. If not specified, returns the object endlessly.
返回一个迭代器,可以指定重复的次数。若不指定次数,则是返回一个无界的迭代器

import itertools

l = [1, 2, 3]
for item in itertools.repeat(l, 3):
    print l,

# 输出结果为: 
[1, 2, 3] [1, 2, 3] [1, 2, 3]

你可能感兴趣的:(python模块之itertools)