def chain(*iterables): # chain('ABC', 'DEF') --> A B C D E F for it in iterables: for element in it: yield element
1
2
3
4
5
如上面的函数定义,chain是第一个迭代数组的每个元素展开,注意到这个是将第一个迭代数组,所以chain展开的是第二维的数组元素,距离来说明:
>>> a=[['abc','def']]>>> for item in itertools.chain(*a):... print item...abcdef>>> b=[[['abc','abc']]]>>> for item in itertools.chain(*b):... print item...['abc', 'abc']
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 for i in indices) while True: for i in reversed(range(r)): if indices != i + n - r: break else: return indices += 1 for j in range(i+1, r): indices[j] = indices[j-1] + 1 yield tuple(pool for i in indices)
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 for i in indices) while True: for i in reversed(range(r)): if indices != n - 1: break else: return indices[i:] = [indices + 1] * (r - i) yield tuple(pool for i in indices)
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
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
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
1
2
3
4
5
6
7
返回符合条件的元素
ifilterfalse
和ifilter相反,返回不符合条件的的元素
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)
1
2
3
4
5
6
7
8
9
10
功能:给iterables的元素添加上function功能
izip
函数定义:
def izip(*iterables): # izip('ABCD', 'xy') --> Ax By iterators = map(iter, iterables) while iterators: yield tuple(map(next, iterators))
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 for i in indices[:r]) while n: for i in reversed(range(r)): cycles -= 1 if cycles == 0: indices[i:] = indices[i+1:] + indices[i:i+1] cycles = n - i else: j = cycles indices, indices[-j] = indices[-j], indices yield tuple(pool for i in indices[:r]) break else: return
函数定义:
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)
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
2
3
4
5
6
7
演示:
>>> for item in itertools.takewhile(lambda x: x<5, [1,4,6,4,1]):... print item...14
1
2
3
4
5
具体的运用例子场景:对于一段文本,我们需要统计每个单词的词频。
方法:运用nltk的分句和分词功能后,运用FreqDist进行统计词频
代码:
import nltkimport itertoolsdef process(str): sentences=itertools.chain(*[nltk.sent_tokenize(str.decode('utf-8').lower())]) words_dict=itertools.chain(*[nltk.word_tokenize(sen) for sen in sentences]) fdist=nltk.FreqDist(words_dict) print fdist.most_common(10) fdist.plot(50,cumulative=True)if __name__=='__main__': text="My interpretation: Gom Jabbar is an ancient non-Latin incantation in HPMORverse which Draco just happened to have researched in old dark tomes, and far predates the writings we're familiar with. Frank Herbert somehow picked up on the phrase, either overhearing it in passing or perhaps by having some actual knowledge of the magical world (e.g. magical relative or somesuch)." process(text)