Itertools 用于高效循環(huán)的迭代函數(shù)集合
itertools.count(start=0,step=1) 創(chuàng)建一個迭代器,從start開始,進行start+step迭代,無限循環(huán)
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())demo
from itertools import *for i in izip(count(1), ['a', 'b', 'c']): PRint i(1, 'a')(2, 'b')(3, 'c')itertools.cycle(iterable)
創(chuàng)建一個迭代器,對iterable中的元素?zé)o限循環(huán)
源碼
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 elementdemo
from itertools import *i = 0for item in cycle(['a', 'b', 'c']): i += 1 if i == 10: break print (i, item)(1, 'a')(2, 'b')(3, 'c')(4, 'a')(5, 'b')(6, 'c')(7, 'a')(8, 'b')(9, 'c')itertools.repeat(objetc[,times])
創(chuàng)建一個迭代器,生成times次的object對象
源碼
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 objectitertools.chain(*iterables)
將多個迭代器連接成一個迭代器
源碼
def chain(*iterables): # chain('ABC', 'DEF') --> A B C D E F for it in iterables: for element in it: yield elementiter.compress(data,selectors)
根據(jù)selectors篩選數(shù)據(jù)
源碼
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)itertools.dropwhile(predicate,iterable)
創(chuàng)建一個迭代器,如果pedicate(item)函數(shù)返回true,就丟棄iterable中的元素,當predicate返回**第一個**false后,生成iterable中的后續(xù)所有項
源碼
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 xitertools.groupby(iterable[,key])
產(chǎn)生一個根據(jù)key分后的值的迭代器
源碼
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)demo
from itertools import *from Operator import itemgetterd = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)di = sorted(d.iteritems(), key=itemgetter(1))for k, g in groupby(di, key=itemgetter(1)): print k, map(itemgetter(0), g)1 ['a', 'c', 'e']2 ['b', 'd', 'f']3 ['g']itertools.ifilter(predicate,iterable)
生成一個迭代器,值為predicate(item)為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 xdemo
from itertools import *def check_item(x): print 'Testing:', x return (x<1)for i in ifilter(check_item, [ -1, 0, 1, 2, 3, 4, 1, -2 ]): print 'Yielding:', iTesting: -1Yielding: -1Testing: 0Yielding: 0Testing: 1Testing: 2Testing: 3Testing: 4Testing: 1Testing: -2Yielding: -2itertools.ifilterfalse(predicate,iterable)
和ifilter相反,當predicate(item)返回值為false的元素生成一個迭代器
源碼
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 xitertools.islice(iterable,start,stop,[,step])
創(chuàng)建一個迭代器,值為在iterable中,從start到stop的值,默認步長為1 start,stop,step不可使用負值,stop不可省略
源碼
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)itertools.imap(function,*iterables)
創(chuàng)建一個迭代器
源碼
def imap(function, *iterables): # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 #(2^3,3^2,10^3) iterables = map(iter, iterables) while True: args = [next(it) for it in iterables] if function is None: yield tuple(args) else: yield function(*args)demo
from itertools import *print 'Doubles:'for i in imap(lambda x:2*x, xrange(5)): print iprint 'Multiples:'for i in imap(lambda x,y:(x, y, x*y), xrange(5), xrange(5,10)): print '%d * %d = %d' % iDoubles:02468Multiples:0 * 5 = 01 * 6 = 62 * 7 = 143 * 8 = 244 * 9 = 36itertools.startmap(function,iterable)
和imap類似,參數(shù)位置不同
源碼
def starmap(function, iterable): # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 # 2^5,362,10^3 for args in iterable: yield function(*args)demo
from itertools import *values = [(0, 5), (1, 6), (2, 7), (3, 8), (4, 9)]for i in starmap(lambda x,y:(x, y, x*y), values): print '%d * %d = %d' % i0 * 5 = 01 * 6 = 62 * 7 = 143 * 8 = 244 * 9 = 36itertools.tee(iterable[,n=2])
把一個迭代器分為n個迭代器,默認n為2
源碼
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)demo
from itertools import *r = islice(count(), 5)i1, i2 = tee(r)for i in i1: print 'i1:', ifor i in i2: print 'i2:', ii1: 0i1: 1i1: 2i1: 3i1: 4i2: 0i2: 1i2: 2i2: 3i2: 4itertools.takewhile(predicate,iterable)
和dropwhile相反,當predicate(item)為false時,停止迭代
源碼
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: breakitertools.izip(*iterable)
將多個迭代器合并成一個元組迭代器,類似內(nèi)置zip()函數(shù)
源碼
izip(iter1, iter2, ... iterN):返回:(it1[0],it2 [0], it3[0], ..), (it1[1], it2[1], it3[1], ..)...def izip(*iterables): # izip('ABCD', 'xy') --> Ax By iterators = map(iter, iterables) while iterators: yield tuple(map(next, iterators))demo
from itertools import *for i in izip([1, 2, 3], ['a', 'b', 'c']): print i(1, 'a')(2, 'b')(3, 'c')itertools.izip_longest(*iterables[, fillvalue])
與izip()相同,但是迭代過程會持續(xù)到所有輸入迭代變量iter1,iter2等都耗盡為止,如果沒有使用fillvalue關(guān)鍵字參數(shù)指定不同的值,則使用None來填充已經(jīng)使用的迭代變量的值。
源碼
class ZipExhausted(Exception): passdef 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: passitertools.product(*iterables[, repeat])
創(chuàng)建一個迭代器,生成表示item1,item2等中的項目的笛卡爾積的元組,repeat是一個關(guān)鍵字參數(shù),指定重復(fù)生成序列的次數(shù)。
源碼
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)demo
import itertoolsa = (1, 2, 3)b = ('A', 'B', 'C')c = itertools.product(a,b)for elem in c: print elem(1, 'A')(1, 'B')(1, 'C')(2, 'A')(2, 'B')(2, 'C')(3, 'A')(3, 'B')(3, 'C')itertools.permutations(iterable[,r])
創(chuàng)建一個迭代器,返回iterable中r個元素的排列組合,r默認為iterable中的長度
源碼
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也可以用product實現(xiàn)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)itertools.combinations(iterable,r)
創(chuàng)建一個迭代器,返回iterable中所有長度為r的子序列,返回的子序列中的項按輸入iterable中的順序排序 (不帶重復(fù)),和perimutations的區(qū)別在于產(chǎn)生的是自序列和元素不重復(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 = 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)#或者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)itertools.combinations_with_replacement(iterable, r)
創(chuàng)建一個迭代器,返回iterable中所有長度為r的子序列,返回的子序列中的項按輸入iterable中的順序排序 (帶重復(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)或者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)新聞熱點
疑難解答