9.7. itertools创建迭代器以实现高效循环的函数

版本 2.3 新增。

这个模块实现了大量的 迭代器 构建代码块,灵感来源于APL、Haskell和SML。每个已经被重新塑造的一种形式适合于 Python。

该模块规范一套核心的快,内存有效的工具,由自己或组合非常有用。它们共同构成了"迭代代数",使它能够纯 Python 中简洁、 高效地构建专门的工具。

例如,sml 语言提供了一个制表工具: tabulate(f)生产序列下探, f(1), ...同样的效果可以在 Python 中实现结合imap()count ()到窗体imap (f, count())

这些工具和内置的对象也能够与operator模块中的高速函数一起工作得很好。例如,乘法运算符可以映射跨两个向量,形成高效的网点产品:总和 (imap (operator.mul, vector1, vector2))

无限迭代器:

Iterator Arguments Results Example
count() start, [step] start, start+step, start+2*step, ... count(10) --> 10 11 12 13 14 ...
cycle() p p0, p1, ... plast, p0, p1, ... cycle('ABCD') --> A B C D A B C D ...
repeat() elem [,n] elem, elem, elem, ... endlessly or up to n times repeat(10, 3) --> 10 10 10

在最短输入序列终止的迭代器:

Iterator Arguments Results Example
chain() p, q, ... p0, p1, ... plast, q0, q1, ... chain('ABC', 'DEF') --> A B C D E F
compress() data, selectors (d[0] if s[0]), (d[1] if s[1]), ... compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
dropwhile() pred, seq seq[n], seq[n+1], starting when pred fails dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
groupby() iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v)  
ifilter() pred, seq elements of seq where pred(elem) is true ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9
ifilterfalse() pred, seq elements of seq where pred(elem) is false ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
islice() seq, [start,] stop [, step] elements from seq[start:stop:step] islice('ABCDEFG', 2, None) --> C D E F G
imap() func, p, q, ... func(p0, q0), func(p1, q1), ... imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
starmap() func, seq func(*seq[0]), func(*seq[1]), ... starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
tee() it, n it1, it2, ... itn splits one iterator into n  
takewhile() pred, seq seq[0], seq[1], until pred fails takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
izip() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip('ABCD', 'xy') --> Ax By
izip_longest() p, q, ... (p[0], q[0]), (p[1], q[1]), ... izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-

组合生成器:

Iterator Arguments Results
product() p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop
permutations() p[, r] r-length tuples, all possible orderings, no repeated elements
combinations() p, r r-length tuples, in sorted order, no repeated elements
combinations_with_replacement() p, r r-length tuples, in sorted order, with repeated elements
product('ABCD', repeat=2)   AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD
permutations('ABCD', 2)   AB AC AD BA BC BD CA CB CD DA DB DC
combinations('ABCD', 2)   AB AC AD BC BD CD
combinations_with_replacement('ABCD', 2)   AA AB AC AD BB BC BD CC CD DD

9.7.1. Itertool functions

下面所有的模块函数都将构造和返回迭代器。一些提供无限长的流,因此只应由截断该流的函数或循环访问它们。

itertools.chain(*iterables)

生成一个迭代器,它返回第一个可迭代序列的元素直到用尽,然后继续下一个可迭代序列,直到所有可迭代序列被用尽。用于将连续的多个序列作为一个单独的序列。相当于:

def chain(*iterables):
    # chain('ABC', 'DEF') --> A B C D E F
    for it in iterables:
        for element in it:
            yield element
classmethod chain.from_iterable(iterable)

chain()的备用构造函数。获取链接输入从一个单一的可迭代参数,进行惰性的计算。大致相当于:

def from_iterable(iterables):
    # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
    for it in iterables:
        for element in it:
            yield element

2.6版中新增。

itertools.combinations(iterable, r)

返回 iterable 中长度为 r 的子序列。

组合是按字典排序顺序排放。所以,如果传入的 iterable 是已排序的,那么返回的组合,也将会是排好序的。

元素根据它们的位置而不是它们的值被视为唯一的。因此,如果输入元素是唯一的,则在每个组合中将不存在重复值。

等价于:

def combinations(iterable, r):
    # combinations('ABCD', 2) --> AB AC AD BC BD CD
    # combinations(range(4), 3) --> 012 013 023 123
    pool = tuple(iterable)
    n = len(pool)
    if r > n:
        return
    indices = range(r)
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != i + n - r:
                break
        else:
            return
        indices[i] += 1
        for j in range(i+1, r):
            indices[j] = indices[j-1] + 1
        yield tuple(pool[i] for i in indices)

Combinations()的代码也可以表示为permutations()的一个子序列中,这是后过滤条目元素并非在排序顺序 (根据他们的位置输入池中):

def combinations(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in permutations(range(n), r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)

返回的项目数是n! /r ! /(n-r) ! 0 < = r < = n或零当r > n

在 2.6 版本新。

itertools.combinations_with_replacement(iterable, r)

从输入长度序列的元素返回r 可迭代允许个别元素重复一次以上。

组合是按字典排序顺序排放。所以,如果输入可迭代是排序,将生产组合元,按排序顺序。

元素就会被作为独特基于它们的位置,不是在它们的价值。因此,如果输入的元素是独一无二的生成的组合也将是唯一的。

等价于:

def combinations_with_replacement(iterable, r):
    # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC
    pool = tuple(iterable)
    n = len(pool)
    if not n and r:
        return
    indices = [0] * r
    yield tuple(pool[i] for i in indices)
    while True:
        for i in reversed(range(r)):
            if indices[i] != n - 1:
                break
        else:
            return
        indices[i:] = [indices[i] + 1] * (r - i)
        yield tuple(pool[i] for i in indices)

Combinations_with_replacement()的代码也可以表示为公式或书签名的一个子序列中,这是后过滤条目元素并非在排序顺序 (根据他们的位置输入池中):

def combinations_with_replacement(iterable, r):
    pool = tuple(iterable)
    n = len(pool)
    for indices in product(range(n), repeat=r):
        if sorted(indices) == list(indices):
            yield tuple(pool[i] for i in indices)

返回的项目数是(n + r-1)! /r ! /(n-1) ! n > 0

在 2.7 版本新。

itertools.compress(data, selectors)

使筛选从数据仅显示那些计算结果为True选择器中有一个对应的元素返回的元素的迭代器。停止时的 数据选择器 可迭代量已用尽。相当于:

def compress(data, selectors):
    # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F
    return (d for d, s in izip(data, selectors) if s)

在 2.7 版本新。

itertools.count(start=0, step=1)

使一个迭代器,返回以n开头的均匀间隔的参数值。常用于作为imap()的参数生成连续的数据点。此外,用于与izip()添加序列号。相当于:

def count(start=0, step=1):
    # count(10) --> 10 11 12 13 14 ...
    # count(2.5, 0.5) -> 2.5 3.0 3.5 ...
    n = start
    while True:
        yield n
        n += step

计数时浮点数,更好的准确性,有时可以代替乘法代码如实现: (start + step * i for i in count())

2.7 版本中的更改:添加步骤参数,允许非整数参数。

itertools.cycle(iterable)

使一个迭代器返回的元素从可迭代和保存的每个副本。从保存的副本,可迭代耗尽时,返回的元素。无限地重复。相当于:

def cycle(iterable):
    # cycle('ABCD') --> A B C D A B C D A B C D ...
    saved = []
    for element in iterable:
        yield element
        saved.append(element)
    while saved:
        for element in saved:
              yield element

请注意,该工具包此成员可能需要大量的辅助存储 (具体取决于可迭代长度)。

itertools.dropwhile(predicate, iterable)

使一个迭代器,从可迭代中删除元素,只要该谓词是真实的;之后,返回的每个元素。请注意,将迭代器不会产生任何输出直到谓词第一次变得是假的所以它可能会有冗长的启动时间。相当于:

def dropwhile(predicate, iterable):
    # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
    iterable = iter(iterable)
    for x in iterable:
        if not predicate(x):
            yield x
            break
    for x in iterable:
        yield x
itertools.groupby(iterable[, key])

生成一个迭代器,此迭代器的元素由连续key和来自the iterable的分组组成。key是一个函数,对每一个元素计算一个key值。如果不指定或者函数默认为一个恒等函数,并返回的原元素(也就是让每一个元素做key)。一般来说,可迭代需求已经要排序上同样的关键作用。

Groupby()操作类似于 Unix 中的uniq筛选器。每次键的值函数 (这就是为什么它是通常需要已排序的数据,使用同样的关键作用) 的变化,它生成中断或新组。这种行为不同于 SQL 的 GROUP BY 的聚合共同的要素,不论其输入的顺序。

返回的组本身就是共享基础与groupby()可迭代的迭代器。因为源共享的当先进的groupby()对象前, 一组不再是可见的。所以,如果以后需要该数据,则应作为一个列表存储:

groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
    groups.append(list(g))      # Store group iterator as a list
    uniquekeys.append(k)

groupby() 等价于:

class groupby(object):
    # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
    # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
    def __init__(self, iterable, key=None):
        if key is None:
            key = lambda x: x
        self.keyfunc = key
        self.it = iter(iterable)
        self.tgtkey = self.currkey = self.currvalue = object()
    def __iter__(self):
        return self
    def next(self):
        while self.currkey == self.tgtkey:
            self.currvalue = next(self.it)    # Exit on StopIteration
            self.currkey = self.keyfunc(self.currvalue)
        self.tgtkey = self.currkey
        return (self.currkey, self._grouper(self.tgtkey))
    def _grouper(self, tgtkey):
        while self.currkey == tgtkey:
            yield self.currvalue
            self.currvalue = next(self.it)    # Exit on StopIteration
            self.currkey = self.keyfunc(self.currvalue)

在 2.4 版本新。

itertools.ifilter(predicate, iterable)

使筛选从可迭代返回只有那些谓词结果为True的元素的迭代器。如果谓词None,返回 true 的项目。相当于:

def ifilter(predicate, iterable):
    # ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9
    if predicate is None:
        predicate = bool
    for x in iterable:
        if predicate(x):
            yield x
itertools.ifilterfalse(predicate, iterable)

使筛选从可迭代返回只有那些谓词对于虚假的元素的迭代器。如果谓词None,返回的都是假的项目。相当于:

def ifilterfalse(predicate, iterable):
    # ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
    if predicate is None:
        predicate = bool
    for x in iterable:
        if not predicate(x):
            yield x
itertools.imap(function, *iterables)

使一个迭代器,计算使用从每个可迭代量参数的函数。如果函数设置为None,则imap()返回参数作为一个元组。喜欢map () ,但停止时最短可迭代是疲惫而不是在没有弥补短可迭代量。差异的原因是无限迭代器的参数通常是错误的map () (因为充分评估输出),但代表提供给imap()的参数的常见、 最有用的方式。相当于:

def imap(function, *iterables):
    # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000
    iterables = map(iter, iterables)
    while True:
        args = [next(it) for it in iterables]
        if function is None:
            yield tuple(args)
        else:
            yield function(*args)
itertools.islice(iterable, stop)
itertools.islice(iterable, start, stop[, step])

使从可迭代中返回所选的元素的迭代器。如果start参数不为零,然后从可迭代的元素被跳过,直到开始。之后,将返回元素连续除非步骤设置高于其中结果被跳过的项目中。如果停止None,则迭代继续直到迭代器是精疲力竭,如果在所有 ;否则,它将停止在指定的位置。不同于常规切片, islice()不支持启动停止步骤为负值。可用于从数据中提取相关的字段的内部结构已被夷为平地 (例如,多线报告可能会列出一个名称字段在每三行)。相当于:

def islice(iterable, *args):
    # islice('ABCDEFG', 2) --> A B
    # islice('ABCDEFG', 2, 4) --> C D
    # islice('ABCDEFG', 2, None) --> C D E F G
    # islice('ABCDEFG', 0, None, 2) --> A C E G
    s = slice(*args)
    it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
    nexti = next(it)
    for i, element in enumerate(iterable):
        if i == nexti:
            yield element
            nexti = next(it)

如果startNone,那么迭代从零开始。如果stepNone,那么步长缺省为 1。

2.5 版本中的更改:startstep接受默认的None值。

itertools.izip(*iterables)

请从每个可迭代量聚合元素的迭代器。zip()除外,它返回一个迭代器,而不是列表。用于锁步迭代在几个可迭代量一次。相当于:

def izip(*iterables):
    # izip('ABCD', 'xy') --> Ax By
    iterators = map(iter, iterables)
    while iterators:
        yield tuple(map(next, iterators))

2.4 版本中的更改:当没有指定可迭代量,则返回一个零长度迭代器而不是引发TypeError异常。

保证可迭代量的左到右计算顺序。这使得一个成语使用izip(*[iter(s)]*n)n 长度分组聚类数据系列。

时你不在乎尾随、 无与伦比的价值观,从更长的时间可迭代量izip()应仅使用长度不等的投入。如果这些价值很重要,则请使用izip_longest()

itertools.izip_longest(*iterables[, fillvalue])

请从每个可迭代量聚合元素的迭代器。如果可迭代量不均,缺少的值是长度的在填补与fillvalue迭代,直到最长可迭代用尽。相当于:

class ZipExhausted(Exception):
    pass

def izip_longest(*args, **kwds):
    # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    counter = [len(args) - 1]
    def sentinel():
        if not counter[0]:
            raise ZipExhausted
        counter[0] -= 1
        yield fillvalue
    fillers = repeat(fillvalue)
    iterators = [chain(it, sentinel(), fillers) for it in args]
    try:
        while iterators:
            yield tuple(map(next, iterators))
    except ZipExhausted:
        pass

如果可迭代量之一就是潜在的无限,然后izip_longest()函数应包有限制 (例如islice()takewhile()) 的调用次数。如果未指定, fillvalue默认为None

在 2.6 版本新。

itertools.permutations(iterable[, r])

返回连续r中元素的长度排列可迭代

如果r未指定或没有,则r默认的长度可迭代并生成所有可能的全长排列。

排列按字典排序顺序排放。所以,如果输入可迭代是排序,置换元组会产生按排序顺序。

元素就会被作为独特基于它们的位置,不是在它们的价值。因此,如果输入的元素是独一无二的每个排列中将不重复的值。

等价于:

def permutations(iterable, r=None):
    # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
    # permutations(range(3)) --> 012 021 102 120 201 210
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    if r > n:
        return
    indices = range(n)
    cycles = range(n, n-r, -1)
    yield tuple(pool[i] for i in indices[:r])
    while n:
        for i in reversed(range(r)):
            cycles[i] -= 1
            if cycles[i] == 0:
                indices[i:] = indices[i+1:] + indices[i:i+1]
                cycles[i] = n - i
            else:
                j = cycles[i]
                indices[i], indices[-j] = indices[-j], indices[i]
                yield tuple(pool[i] for i in indices[:r])
                break
        else:
            return

作为子序列的公式或书签名,筛选,以便排除具有重复的元素 (从相同位置输入池中的那些) 的条目,也可以表达permutations()的代码:

def permutations(iterable, r=None):
    pool = tuple(iterable)
    n = len(pool)
    r = n if r is None else r
    for indices in product(range(n), repeat=r):
        if len(set(indices)) == r:
            yield tuple(pool[i] for i in indices)

返回的项目数是n! /(n-r) ! 0 < = r < = n或零当r > n

在 2.6 版本新。

itertools.product(*iterables[, repeat])

笛卡儿积的输入可迭代量。

相当于嵌套 for 循环在生成器表达式中。例如,产品 (A, B)返回相同((x,y) x A y B)

嵌套的循环周期像里程表前进的每次迭代的最右边的元素。这种模式创建字典排序以便如果输入可迭代量进行排序,按排序顺序排放产品元。

若要计算可迭代与本身的产品,使用可选的重复的关键字参数指定重复的次数。例如,产品 (A, 重复 = 4)意思相同或产品 (A, A, A、 A)

此函数等同于下面的代码,只是实际执行没有建立在内存中的中间结果:

def product(*args, **kwds):
    # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
    # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
    pools = map(tuple, args) * kwds.get('repeat', 1)
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y in pool]
    for prod in result:
        yield tuple(prod)

在 2.6 版本新。

itertools.repeat(object[, times])

使一个迭代器,一次次返回对象无限期地运行,除非指定了时间参数。作为imap()参数用于不变函数参数。此外用izip()来创建元组记录中的常数字段。等价于:

def repeat(object, times=None):
    # repeat(10, 3) --> 10 10 10
    if times is None:
        while True:
            yield object
    else:
        for i in xrange(times):
            yield object

重复一个常见用途是提供一个流到imapzip的常量值:

>>> list(imap(pow, xrange(10), repeat(2)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
itertools.starmap(function, iterable)

使一个迭代器,计算使用所得可迭代的参数的函数。当变量参数已分组在元组从单个可迭代 (数据已经"预压缩") 中使用而不是imap() Imap()starmap()之间的区别与平行function(a,b)function(*c)之间的区别。等价于:

def starmap(function, iterable):
    # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
    for args in iterable:
        yield function(*args)

2.6 版本中的更改:以前, starmap()要求函数的参数是元组。现在,任何可迭代被允许。

itertools.takewhile(predicate, iterable)

使只要谓词是真正从可迭代返回元素的迭代器。等价于:

def takewhile(predicate, iterable):
    # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
    for x in iterable:
        if predicate(x):
            yield x
        else:
            break
itertools.tee(iterable[, n=2])

N独立迭代器从返回单个可迭代。等价于:

def tee(iterable, n=2):
    it = iter(iterable)
    deques = [collections.deque() for i in range(n)]
    def gen(mydeque):
        while True:
            if not mydeque:             # when the local deque is empty
                newval = next(it)       # fetch a new value and
                for d in deques:        # load it to all the deques
                    d.append(newval)
            yield mydeque.popleft()
    return tuple(gen(d) for d in deques)

一旦tee()作出了分裂,原可迭代不应使用任何其他 ;否则为可迭代可以获得高级无三通对象被告知。

这 itertool 可能需要大量的辅助存储 (具体取决于有多少临时数据需要存储)。一般情况下,如果另一个迭代使用的大部分或全部数据的另一个迭代开始之前,它是更快地使用list ()而不是tee()

在 2.4 版本新。

9.7.2. Recipes

本节说明用于创建扩展的工具集,利用现有的 itertools 作为构建基块的食谱。

扩展工具提供与底层工具集相同的高性能。优越的存储器性能通过一次一个处理元件来保持,而不是一次性地将整个可迭代器带入存储器。通过以有助于消除临时变量的功能样式将工具链接在一起,使代码量保持较小。通过使用for循环和生成器优选“向量化”构造块来保持高速度,这引起解释器开销。

def take(n, iterable):
    "Return first n items of the iterable as a list"
    return list(islice(iterable, n))

def tabulate(function, start=0):
    "Return function(0), function(1), ..."
    return imap(function, count(start))

def consume(iterator, n):
    "Advance the iterator n-steps ahead. If n is none, consume entirely."
    # Use functions that consume iterators at C speed.
    if n is None:
        # feed the entire iterator into a zero-length deque
        collections.deque(iterator, maxlen=0)
    else:
        # advance to the empty slice starting at position n
        next(islice(iterator, n, n), None)

def nth(iterable, n, default=None):
    "Returns the nth item or a default value"
    return next(islice(iterable, n, None), default)

def quantify(iterable, pred=bool):
    "Count how many times the predicate is true"
    return sum(imap(pred, iterable))

def padnone(iterable):
    """Returns the sequence elements and then returns None indefinitely.

    Useful for emulating the behavior of the built-in map() function.
    """
    return chain(iterable, repeat(None))

def ncycles(iterable, n):
    "Returns the sequence elements n times"
    return chain.from_iterable(repeat(tuple(iterable), n))

def dotproduct(vec1, vec2):
    return sum(imap(operator.mul, vec1, vec2))

def flatten(listOfLists):
    "Flatten one level of nesting"
    return chain.from_iterable(listOfLists)

def repeatfunc(func, times=None, *args):
    """Repeat calls to func with specified arguments.

    Example:  repeatfunc(random.random)
    """
    if times is None:
        return starmap(func, repeat(args))
    return starmap(func, repeat(args, times))

def pairwise(iterable):
    "s -> (s0,s1), (s1,s2), (s2, s3), ..."
    a, b = tee(iterable)
    next(b, None)
    return izip(a, b)

def grouper(iterable, n, fillvalue=None):
    "Collect data into fixed-length chunks or blocks"
    # grouper('ABCDEFG', 3, 'x') --> ABC DEF Gxx
    args = [iter(iterable)] * n
    return izip_longest(fillvalue=fillvalue, *args)

def roundrobin(*iterables):
    "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
    # Recipe credited to George Sakkis
    pending = len(iterables)
    nexts = cycle(iter(it).next for it in iterables)
    while pending:
        try:
            for next in nexts:
                yield next()
        except StopIteration:
            pending -= 1
            nexts = cycle(islice(nexts, pending))

def powerset(iterable):
    "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)"
    s = list(iterable)
    return chain.from_iterable(combinations(s, r) for r in range(len(s)+1))

def unique_everseen(iterable, key=None):
    "List unique elements, preserving order. Remember all elements ever seen."
    # unique_everseen('AAAABBBCCDAABBB') --> A B C D
    # unique_everseen('ABBCcAD', str.lower) --> A B C D
    seen = set()
    seen_add = seen.add
    if key is None:
        for element in ifilterfalse(seen.__contains__, iterable):
            seen_add(element)
            yield element
    else:
        for element in iterable:
            k = key(element)
            if k not in seen:
                seen_add(k)
                yield element

def unique_justseen(iterable, key=None):
    "List unique elements, preserving order. Remember only the element just seen."
    # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B
    # unique_justseen('ABBCcAD', str.lower) --> A B C A D
    return imap(next, imap(itemgetter(1), groupby(iterable, key)))

def iter_except(func, exception, first=None):
    """ Call a function repeatedly until an exception is raised.

    Converts a call-until-exception interface to an iterator interface.
    Like __builtin__.iter(func, sentinel) but uses an exception instead
    of a sentinel to end the loop.

    Examples:
        bsddbiter = iter_except(db.next, bsddb.error, db.first)
        heapiter = iter_except(functools.partial(heappop, h), IndexError)
        dictiter = iter_except(d.popitem, KeyError)
        dequeiter = iter_except(d.popleft, IndexError)
        queueiter = iter_except(q.get_nowait, Queue.Empty)
        setiter = iter_except(s.pop, KeyError)

    """
    try:
        if first is not None:
            yield first()
        while 1:
            yield func()
    except exception:
        pass

def random_product(*args, **kwds):
    "Random selection from itertools.product(*args, **kwds)"
    pools = map(tuple, args) * kwds.get('repeat', 1)
    return tuple(random.choice(pool) for pool in pools)

def random_permutation(iterable, r=None):
    "Random selection from itertools.permutations(iterable, r)"
    pool = tuple(iterable)
    r = len(pool) if r is None else r
    return tuple(random.sample(pool, r))

def random_combination(iterable, r):
    "Random selection from itertools.combinations(iterable, r)"
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.sample(xrange(n), r))
    return tuple(pool[i] for i in indices)

def random_combination_with_replacement(iterable, r):
    "Random selection from itertools.combinations_with_replacement(iterable, r)"
    pool = tuple(iterable)
    n = len(pool)
    indices = sorted(random.randrange(n) for i in xrange(r))
    return tuple(pool[i] for i in indices)

def tee_lookahead(t, i):
    """Inspect the i-th upcomping value from a tee object
       while leaving the tee object at its current position.

       Raise an IndexError if the underlying iterator doesn't
       have enough values.

    """
    for value in islice(t.__copy__(), i, None):
        return value
    raise IndexError(i)

请注意,许多上述食谱可以优化全局查找替换本地变量定义为默认值。例如, dotproduct食谱可以写成:

def dotproduct(vec1, vec2, sum=sum, imap=imap, mul=operator.mul):
    return sum(imap(mul, vec1, vec2))