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]
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值