首页 > 学院 > 开发设计 > 正文

Python Itertools

2019-11-14 10:24:49
字体:
来源:转载
供稿:网友

Itertools 用于高效循环的迭代函数集合

itertools.count(start=0,step=1) 创建一个迭代器,从start开始,进行start+step迭代,无限循环

源码

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)

创建一个迭代器,对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

demo

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])

创建一个迭代器,生成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 object

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

iter.compress(data,selectors)

根据selectors筛选数据

源码

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)

创建一个迭代器,如果pedicate(item)函数返回true,就丢弃iterable中的元素,当predicate返回**第一个**false后,生成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分后的值的迭代器

源码

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 x

demo

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: -2

itertools.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 x

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

创建一个迭代器,值为在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)

创建一个迭代器

源码

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 = 36

itertools.startmap(function,iterable)

和imap类似,参数位置不同

源码

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 = 36

itertools.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: 4

itertools.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: break

itertools.izip(*iterable)

将多个迭代器合并成一个元组迭代器,类似内置zip()函数

源码

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()相同,但是迭代过程会持续到所有输入迭代变量iter1,iter2等都耗尽为止,如果没有使用fillvalue关键字参数指定不同的值,则使用None来填充已经使用的迭代变量的值。

源码

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: pass

itertools.product(*iterables[, repeat])

创建一个迭代器,生成表示item1,item2等中的项目的笛卡尔积的元组,repeat是一个关键字参数,指定重复生成序列的次数。

源码

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])

创建一个迭代器,返回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实现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)

创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序 (不带重复),和perimutations的区别在于产生的是自序列和元素不重复?

源码

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)

创建一个迭代器,返回iterable中所有长度为r的子序列,返回的子序列中的项按输入iterable中的顺序排序 (带重复)

源码

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)
发表评论 共有条评论
用户名: 密码:
验证码: 匿名发表