itertools模塊翻譯

1 itertools函數(shù)

以下模塊級別函數(shù)都構(gòu)造并返回iterator祠够。有些iterator是無限長度的流九妈,因此它們只能被截斷流的函數(shù)或循環(huán)訪問泰演。

1.1 itertools.accumelate(iterable[, func])

版本3.2中新增。
版本3.3中修改:添加可以參數(shù)*func*搁凸。

創(chuàng)建一個iterator媚值,返回累積和,或者其它二元函數(shù)的累積結(jié)果(通過可選的func參數(shù))护糖。參數(shù)func應(yīng)該能接收兩個參數(shù)褥芒。iterable中的元素可以是func函數(shù)接受的任何類型。(例如嫡良,使用默認(rèn)的加法操作锰扶,元素可以是任何可做加法的類型,包括DecimalFraction寝受。)如果輸入的iterable為空坷牛,則輸出的iterable也為空。

大致等價于:

def accumulate(iterable, func=operator.add):
    'Return running totals'
    # accumulate([1, 2, 3, 4, 5]) --> 1 3 6 10 15
    # accumulate([1, 2, 3, 4, 5], operator.mul) --> 1 2 6 24 120
    it = iter(iterable)
    try:
        total = next(it)
    except StopIteration:
        return
    yield total
    for element in it:
        total = func(total, element)
        yield total

func參數(shù)有很多種用法很澄【┤颍可以設(shè)置為min()求最小值颜及,max()求最大值,或者operator.mul()求積蹂楣。分期還款表可以通過積累利息和申請付款構(gòu)建俏站。在iterable中提供初始值,并只使用func參數(shù)中計算的總數(shù)捐迫,可以建模一階遞歸關(guān)系:

>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
>>> list(accumulate(data, operator.mul))
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
>>> list(accumulate(data, max)) 
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]

# 1000元的利率是5%乾翔,分期還4年爱葵,每年還90
>>> cashflows = [1000, -90, -90, -90, -90]
>>> list(accumulate(cashflows, lambda bal, pmt: bal*1.05+pmt))
[1000, 960.0, 918.0, 873.9000000000001, 827.5950000000001]

# Chaotic recurrence relation https://en.wikipedia.org/wiki/Logistic_map
>>> logistic_map = lambda x, _:  r * x * (1 - x)
>>> r = 3.8
>>> x0 = 0.4
>>> inputs = repeat(x0, 36)     # only the initial value is used
>>> [format(x, '.2f') for x in accumulate(inputs, logistic_map)]
['0.40', '0.91', '0.30', '0.81', '0.60', '0.92', '0.29', '0.79', '0.63',
 '0.88', '0.39', '0.90', '0.33', '0.84', '0.52', '0.95', '0.18', '0.57',
 '0.93', '0.25', '0.71', '0.79', '0.63', '0.88', '0.39', '0.91', '0.32',
 '0.83', '0.54', '0.95', '0.20', '0.60', '0.91', '0.30', '0.80', '0.60']

只返回最終累積值的相似函數(shù)施戴,請參考functools.reduce()

1.2 itertools.chain(*iterables)

創(chuàng)建一個iterator萌丈,從第一個iterable中返回元素赞哗,直到最后一個,然后處理下一個iterable辆雾,直到最后一個iterable肪笋。用于將連續(xù)序列作為單個序列處理。大致等價于:

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

1.3 類方法:chain.from_iterable(iterable)

chain()的替代構(gòu)造函數(shù)度迂。通過延遲計算藤乙,從單個可迭代參數(shù)中獲得鏈接輸入。大致等價于:

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

1.4 itertools.combinations(iterable, r)

從輸入的iterable中返回長度為r的元素子序列惭墓。

組合以字母順序排列坛梁。因此,如果輸入的iterable是有序的腊凶,那么產(chǎn)生的組合元組也是有序的划咐。

元素根據(jù)它們的位置,而不是它們的值來確定唯一性钧萍。因此褐缠,如果輸入的元素是唯一的,那么每個組合中不會有重復(fù)的元素风瘦。

大致等價于:

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 = list(range(r))
    yield tuple(pool[i] for i 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)

過濾掉無序元素的條目后(根據(jù)它們在pool中的位置)队魏,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)

當(dāng)0 <= r <= n時,返回的項目數(shù)量為n! / r! / (n-r)!万搔,當(dāng)r > n時返回0器躏。

1.5 itertools.combinations_with_replacement(iterable, r)

版本3.1中新增。

從輸入的iterable中返回長度為r的元素子序列蟹略,允許單個元素重復(fù)多次登失。

組合以字母順序排列。因此挖炬,如果輸入的iterable是有序的揽浙,那么產(chǎn)生的組合元組也是有序的。

元素根據(jù)它們的位置,而不是它們的值來確定唯一性馅巷。因此膛虫,如果輸入的元素是唯一的,那么每個組合中不會有重復(fù)的元素钓猬。

大致等價于:

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)

過濾掉無序元素的條目后(根據(jù)它們在pool中的位置)稍刀,combinations_with_replacement()的代碼可以使用product()的子序列表示:

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)

當(dāng)n > 0時,返回的項目數(shù)量為(n+r-1)! / r! / (n-1)!敞曹,當(dāng)r > n時返回0账月。

1.6 itertools.compress(data, selectors)

版本3.1中新增。

創(chuàng)建一個從data中過濾元素的iterator澳迫,只返回在selector中計算結(jié)果為True的對應(yīng)元素局齿。當(dāng)data或者selector迭代完最后一個元素后停止。大致等價于:

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

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

版本3.1中修改:添加step參數(shù)橄登,并允許非整數(shù)參數(shù)抓歼。

創(chuàng)建一個iterator,從數(shù)字start開始拢锹,返回均勻間隔的值谣妻。通常用于map()的參數(shù),生成連續(xù)的數(shù)據(jù)點卒稳。同時蹋半,也可與zip()一起使用,用來添加數(shù)列展哭。大致等價與:

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:
        yiled n
        n += step

使用浮點數(shù)計數(shù)時湃窍,使用乘法,例如:start + step * i for i in count()匪傍,可以獲得更高的精度您市。

1.8 itertools.cycle(iterable)

創(chuàng)建一個iterator,從iterable返回元素役衡,并保存每個元素的副本茵休。當(dāng)iterable耗盡時,從保存的副本中返回元素手蝎。無限重復(fù)榕莺。大致等價與:

def cycle(iterable):
    # cycle('ABCD') --> 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

注意,這個工具可能需要大量的存儲空間(取決于iterable的長度)棵介。**

1.9 itertools.dropwhile(predicate, iterable)

創(chuàng)建一個iterator钉鸯, 只要predicate為真,就從iterable中丟棄元素邮辽;然后返回每個元素唠雕。注意贸营,該iteratorpredicate首次變?yōu)?em>False之前,不會生成任何輸出岩睁,因此可能有較長的啟動時間钞脂。大致等價于:

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):
            yiled x
            break
    for x in iterable:
        yield x

1.10 itertools.filterfalse(predicate, iterable)

創(chuàng)建一個從iterable中過濾元素的iterator,只返回predicateFalse的元素捕儒。如果predicateNone冰啃,返回為False的元素。大致等價于:

def filterfalse(predicate, iterable):
    # filterfalse(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

1.11 itertools.groupby(iterable, key=None)

創(chuàng)建一個iterator刘莹,從iterable中返回連續(xù)的鍵和組阎毅。參數(shù)key是計算每個元素鍵值的函數(shù)。如果沒有指定栋猖,或者為None净薛,默認(rèn)恒等函數(shù)(identity function)汪榔,并返回未改變的元素蒲拉。通常,iterable已經(jīng)按同一個key函數(shù)排序了痴腌。

返回的組本身是一個iterator雌团,與groupby()共享底層的iterable。因為源是共享的士聪,當(dāng)groupby()對象被提前時锦援,上一個group不再可見。因此剥悟,如果之后需要數(shù)據(jù)灵寺,應(yīng)該保存在一個列表中:

groups = []
uniquekeys = []
data = sorted(data, key=keyfunc)
for k, g in groupby(data, keyfunc):
    groups.append(list(g))
    uniquekeys.append(k)

groupby()大致等價于:

class groupby:
    # [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)
            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
            try:
                self.currvalue = next(self.it)
            except StopIteration:
                return
            self.currkey = self.keyfunc(self.currvalue)

1.12 itertools.islice(iterable, stop) & itertools.islice(iterable, start, stop[, step])

創(chuàng)建一個iterator,從iterable中返回選中的元素区岗。如果start不為0略板,那么start之前的元素會被跳過。接著慈缔,會返回連續(xù)的元素叮称,除非step設(shè)置為大于0,導(dǎo)致元素被跳過藐鹤。如果stopNone瓤檐,則會迭代到最后一個元素;否則會在指定位置停止娱节。不同于常規(guī)的切片挠蛉,islice()startstopstep不支持負數(shù)肄满∏垂牛可用于從內(nèi)部結(jié)構(gòu)扁平化的數(shù)據(jù)中提取相關(guān)的字段(例如绍移,一個多行的報告可能每三行列出一個名稱字段)。大致等價于:

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(range(s.start or 0, s.stop or sys.maxsize, s.step or 1))
    try:
        nexti = next(it)
    except StopIteraction:
        return
    for i, element in enumerate(iterable):
        if i == nexti:
            yield element
            nexti = next(it)

如果startNone讥电,則從0開始迭代蹂窖。如果stepNone,默認(rèn)值為1恩敌。

1.13 itertools.permutations(iterable, r=None)

返回iterable中長度為r的連續(xù)排列瞬测。

如果指定r,或者為None纠炮,默認(rèn)值為iterable的長度月趟,同時生成所有可能的等長排列。

組合以字母順序排列恢口。因此孝宗,如果輸入的iterable是有序的,那么產(chǎn)生的組合元組也是有序的耕肩。

元素根據(jù)它們的位置因妇,而不是它們的值來確定唯一性。因此猿诸,如果輸入的元素是唯一的婚被,那么每個組合中不會有重復(fù)的元素。

大致等價于:

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 = list(range(n))
    cycles = list(range(n ,n-r, -1))
    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

過濾掉重復(fù)元素的條目后(在輸入的pool中有相同的位置)梳虽,permutations()的代碼可以使用product()的子序列表示:

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

當(dāng)0 <= r <= n時址芯,返回的項目數(shù)量為n! / (n-r)!,當(dāng)r > n時返回0窜觉。

1.14 itertools.product(*iterables, repeat=1)

輸入iterables的笛卡爾積谷炸。

大致等價于在生成器表達式中內(nèi)嵌for循環(huán)。例如禀挫,product(A, B)返回值與((x, y) for x in A for y in B)相同旬陡。

循環(huán)嵌套想里程表,最右邊的元素在每次迭代中前進特咆。這種模式以字母順序排列季惩。因此,如果輸入的iterable是有序的腻格,那么產(chǎn)生的組合元組也是有序的画拾。

指定可選關(guān)鍵字參數(shù)repeat,可以計算iterable與自身的乘積菜职。例如青抛,product(A, repeat=4)表示product(A, A, A, A)

該函數(shù)大致等價于以下代碼酬核,除了實際的實現(xiàn)中不會在內(nèi)存中創(chuàng)建中間結(jié)果:

def product(*args, repeat=1):
    # 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 = [tuple(pool) for pool in args] * repeat
    result = [[]]
    for pool in pools:
        result = [x+[y] for x in result for y pool]
    for prod in result:
        yield tuple(prod)

1.15 itertools.repeat(object[, times])

創(chuàng)建一個重復(fù)返回objectiterator蜜另。無限返回适室,除非指定了times參數(shù)。作為map()的不變參數(shù)举瑰,來調(diào)用函數(shù)捣辆。也可用于zip(),創(chuàng)建元組的不變部分此迅。

大致等價于:

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

常用用法是為mapzip提供常量值的流:

>>> list(map(pow, range(10), repeat(2)))
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]

1.16 itertools.starmap(function, iterable)

創(chuàng)建一個iterator汽畴,使用從iterable中獲得的參數(shù)計算function。當(dāng)參數(shù)已經(jīng)從單個iterable中分組為tuple(數(shù)據(jù)已經(jīng)被pre-zipped)時耸序,用于代替map()忍些。map()starmap()的區(qū)別與function(a, b)function(*c)的區(qū)別一致。大致相當(dāng)于:

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

1.17 itertools.takewhile(predicate, iterable)

創(chuàng)建一個iterator坎怪,只要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

1.18 itertools.tee(iterable, n=2)

從單個iterable中返回n個獨立的iterator搅窿。

以下代碼幫助解釋tee做了什么(盡管實際的實現(xiàn)更復(fù)雜嘁酿,并且只有一個底層的FIFO隊列)。

大致等價于:

def tee(iterable, n=2):
    it = iter(iterable)
    deques = [collections.deque() for i in range(n)]
    def gen(mydeque):
        while True:
            if not mydeque:
                try:
                    newval = next(it)
                except StopIteration:
                    return
                for d in deques:
                    d.append(newval)
            yield mydeque.popleft()
    return tuple(gen(d) for d in deques)

一旦tee()做了拆分戈钢,原始的iterable不應(yīng)該在其它任何地方使用痹仙;否則是尔,iterable前進時不會通知tee殉了。

注意,這個工具可能需要大量的存儲空間(取決于需要存儲多少臨時數(shù)據(jù)的長度)拟枚。通常薪铜,如果一個iterator在另一個iterator開始之前使用大多數(shù)或所有數(shù)據(jù),使用list()會比tee()更快恩溅。

1.19 itertools.zip_longest(*iterables, fillvalue=None)

創(chuàng)建一個iterator隔箍,匯集iterable中的每一個元素。如果每個iterable的長度不相等脚乡,缺失的值使用fillvalue填充蜒滩。迭代知道最長的iterable耗盡為止。大致等價于:

class ZipExhausted(Exception):
    pass
    
def zip_longest(*args, **kwds):
    # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
    fillvalue = kwds.get('fillvalue')
    counter = len(args) - 1
    def sentinel():
        nonlocal counter
        if not counter:
            raise ZipExhausted
        counter -= 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

如果其中一個iterable可能無窮大奶稠,則zip_longest()函數(shù)應(yīng)該使用限制數(shù)量的調(diào)用(例如islice()或者takewhile())俯艰。如果沒有指定,fillvalue默認(rèn)值為None锌订。

2 itertools小竅門

本節(jié)使用itertools作為創(chuàng)建擴展工具集的構(gòu)件竹握。

擴展工具提供與底層工具集相同的高性能。通過一次處理一個元素辆飘,而不是一次把整個iterable放入內(nèi)存啦辐,保持了內(nèi)存的高性能谓传。在有助于消除臨時變量的函數(shù)樣式中,把工具鏈接在一起芹关,使代碼量保持較小续挟。使用向量化構(gòu)件,而不是導(dǎo)致解釋器開銷的for循環(huán)和生成器侥衬,來保持快速執(zhí)行代碼庸推。

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 map(function, count(start))

def tail(n, iterable):
    "Return an iterator over the last n items"
    # tail(3, 'ABCDEFG') --> E F G
    return iter(collections.deque(iterable, maxlen=n))

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 all_equal(iterable):
    "Returns True if all the elements are equal to each other"
    g = groupby(iterable)
    return next(g, True) and not next(g, False)

def quantify(iterable, pred=bool):
    "Count how many times the predicate is true"
    return sum(map(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(map(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 zip(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 zip_longest(*args, fillvalue=fillvalue)

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 partition(pred, iterable):
    'Use a predicate to partition entries into false entries and true entries'
    # partition(is_odd, range(10)) --> 0 2 4 6 8   and  1 3 5 7 9
    t1, t2 = tee(iterable)
    return filterfalse(pred, t1), filter(pred, t2)

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 filterfalse(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 map(next, map(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 builtins.iter(func, sentinel) but uses an exception instead
    of a sentinel to end the loop.

    Examples:
        iter_except(functools.partial(heappop, h), IndexError)   # priority queue iterator
        iter_except(d.popitem, KeyError)                         # non-blocking dict iterator
        iter_except(d.popleft, IndexError)                       # non-blocking deque iterator
        iter_except(q.get_nowait, Queue.Empty)                   # loop over a producer Queue
        iter_except(s.pop, KeyError)                             # non-blocking set iterator

    """
    try:
        if first is not None:
            yield first()            # For database APIs needing an initial cast to db.first()
        while True:
            yield func()
    except exception:
        pass

def first_true(iterable, default=False, pred=None):
    """Returns the first true value in the iterable.

    If no true value is found, returns *default*

    If *pred* is not None, returns the first item
    for which pred(item) is true.

    """
    # first_true([a,b,c], x) --> a or b or c or x
    # first_true([a,b], x, f) --> a if f(a) else b if f(b) else x
    return next(filter(pred, iterable), default)

def random_product(*args, repeat=1):
    "Random selection from itertools.product(*args, **kwds)"
    pools = [tuple(pool) for pool in args] * repeat
    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(range(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 range(r))
    return tuple(pool[i] for i in indices)

注意,通過局部變量定義為默認(rèn)值浇冰,來代替全局查找贬媒,以上大部分方法都可以優(yōu)化。例如肘习,dotproduct方法可以重寫為:

def dotproduct(vec1, vec2, sum=sum, map=map, mul=operator.mul):
    return sum(map(mul, vec1, vec2))
最后編輯于
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請聯(lián)系作者
  • 序言:七十年代末际乘,一起剝皮案震驚了整個濱河市,隨后出現(xiàn)的幾起案子漂佩,更是在濱河造成了極大的恐慌脖含,老刑警劉巖,帶你破解...
    沈念sama閱讀 218,036評論 6 506
  • 序言:濱河連續(xù)發(fā)生了三起死亡事件投蝉,死亡現(xiàn)場離奇詭異养葵,居然都是意外死亡,警方通過查閱死者的電腦和手機瘩缆,發(fā)現(xiàn)死者居然都...
    沈念sama閱讀 93,046評論 3 395
  • 文/潘曉璐 我一進店門关拒,熙熙樓的掌柜王于貴愁眉苦臉地迎上來,“玉大人庸娱,你說我怎么就攤上這事着绊。” “怎么了熟尉?”我有些...
    開封第一講書人閱讀 164,411評論 0 354
  • 文/不壞的土叔 我叫張陵归露,是天一觀的道長。 經(jīng)常有香客問我斤儿,道長剧包,這世上最難降的妖魔是什么? 我笑而不...
    開封第一講書人閱讀 58,622評論 1 293
  • 正文 為了忘掉前任往果,我火速辦了婚禮疆液,結(jié)果婚禮上,老公的妹妹穿的比我還像新娘棚放。我一直安慰自己枚粘,他們只是感情好,可當(dāng)我...
    茶點故事閱讀 67,661評論 6 392
  • 文/花漫 我一把揭開白布飘蚯。 她就那樣靜靜地躺著馍迄,像睡著了一般福也。 火紅的嫁衣襯著肌膚如雪。 梳的紋絲不亂的頭發(fā)上攀圈,一...
    開封第一講書人閱讀 51,521評論 1 304
  • 那天暴凑,我揣著相機與錄音,去河邊找鬼赘来。 笑死现喳,一個胖子當(dāng)著我的面吹牛,可吹牛的內(nèi)容都是我干的犬辰。 我是一名探鬼主播嗦篱,決...
    沈念sama閱讀 40,288評論 3 418
  • 文/蒼蘭香墨 我猛地睜開眼,長吁一口氣:“原來是場噩夢啊……” “哼幌缝!你這毒婦竟也來了灸促?” 一聲冷哼從身側(cè)響起,我...
    開封第一講書人閱讀 39,200評論 0 276
  • 序言:老撾萬榮一對情侶失蹤涵卵,失蹤者是張志新(化名)和其女友劉穎浴栽,沒想到半個月后,有當(dāng)?shù)厝嗽跇淞掷锇l(fā)現(xiàn)了一具尸體轿偎,經(jīng)...
    沈念sama閱讀 45,644評論 1 314
  • 正文 獨居荒郊野嶺守林人離奇死亡典鸡,尸身上長有42處帶血的膿包…… 初始之章·張勛 以下內(nèi)容為張勛視角 年9月15日...
    茶點故事閱讀 37,837評論 3 336
  • 正文 我和宋清朗相戀三年,在試婚紗的時候發(fā)現(xiàn)自己被綠了坏晦。 大學(xué)時的朋友給我發(fā)了我未婚夫和他白月光在一起吃飯的照片萝玷。...
    茶點故事閱讀 39,953評論 1 348
  • 序言:一個原本活蹦亂跳的男人離奇死亡,死狀恐怖英遭,靈堂內(nèi)的尸體忽然破棺而出间护,到底是詐尸還是另有隱情,我是刑警寧澤挖诸,帶...
    沈念sama閱讀 35,673評論 5 346
  • 正文 年R本政府宣布,位于F島的核電站法精,受9級特大地震影響多律,放射性物質(zhì)發(fā)生泄漏。R本人自食惡果不足惜搂蜓,卻給世界環(huán)境...
    茶點故事閱讀 41,281評論 3 329
  • 文/蒙蒙 一狼荞、第九天 我趴在偏房一處隱蔽的房頂上張望。 院中可真熱鬧帮碰,春花似錦相味、人聲如沸。這莊子的主人今日做“春日...
    開封第一講書人閱讀 31,889評論 0 22
  • 文/蒼蘭香墨 我抬頭看了看天上的太陽拓巧。三九已至,卻和暖如春一死,著一層夾襖步出監(jiān)牢的瞬間肛度,已是汗流浹背。 一陣腳步聲響...
    開封第一講書人閱讀 33,011評論 1 269
  • 我被黑心中介騙來泰國打工投慈, 沒想到剛下飛機就差點兒被人妖公主榨干…… 1. 我叫王不留承耿,地道東北人。 一個月前我還...
    沈念sama閱讀 48,119評論 3 370
  • 正文 我出身青樓伪煤,卻偏偏與公主長得像加袋,于是被迫代替她去往敵國和親。 傳聞我的和親對象是個殘疾皇子抱既,可洞房花燭夜當(dāng)晚...
    茶點故事閱讀 44,901評論 2 355

推薦閱讀更多精彩內(nèi)容