itertools module


In [1]:
from itertools import *

1 chain(*iterables)

Make an iterator that returns elements from the first iterable until it is exhausted, then proceeds to the next iterable, until all of the iterables are exhausted.


In [3]:
for value in chain('gau', 'fung'):
    print value,


g a u f u n g

2 combinations(iterable, r)

Return r length subsequences of elements from the input iterable.


In [6]:
for value in combinations('gaufung', 5):
    print value


('g', 'a', 'u', 'f', 'u')
('g', 'a', 'u', 'f', 'n')
('g', 'a', 'u', 'f', 'g')
('g', 'a', 'u', 'u', 'n')
('g', 'a', 'u', 'u', 'g')
('g', 'a', 'u', 'n', 'g')
('g', 'a', 'f', 'u', 'n')
('g', 'a', 'f', 'u', 'g')
('g', 'a', 'f', 'n', 'g')
('g', 'a', 'u', 'n', 'g')
('g', 'u', 'f', 'u', 'n')
('g', 'u', 'f', 'u', 'g')
('g', 'u', 'f', 'n', 'g')
('g', 'u', 'u', 'n', 'g')
('g', 'f', 'u', 'n', 'g')
('a', 'u', 'f', 'u', 'n')
('a', 'u', 'f', 'u', 'g')
('a', 'u', 'f', 'n', 'g')
('a', 'u', 'u', 'n', 'g')
('a', 'f', 'u', 'n', 'g')
('u', 'f', 'u', 'n', 'g')

3 combinations_with_replacement(iterable, r)

Return r length subsequences of elements from the input iterable allowing individual elements to be repeated more than once.


In [9]:
for value in combinations_with_replacement('abc', 2):
    print value


('a', 'a')
('a', 'b')
('a', 'c')
('b', 'b')
('b', 'c')
('c', 'c')

4 compress(data, selectors)

Make an iterator that filters elements from data returning only those that have a corresponding element in selectors that evaluates to True. Stops when either the data or selectors iterables has been exhausted.


In [12]:
for value in compress('gaufung',[1,0,0,1,0,0,0]):
    print value,


g f

5 count(start=0, step=1)

Make an iterator that returns evenly spaced values starting with n.

6 cycle(iterable)

Make an iterator returning elements from the iterable and saving a copy of each. When the iterable is exhausted, return elements from the saved copy. Repeats indefinitely.

7 dropwhile(predicate, iterable)

Make an iterator that drops elements from the begining of iterable as long as the predicate is true; afterwards, returns every element. Note, the iterator does not produce any output until the predicate first becomes false, so it may have a lengthy start-up time.


In [28]:
for value in dropwhile(lambda x: x>5, [7,4,6,4,1]):
    print value,


4 6 4 1

8 groupby(iterable[, key])

Make an iterator that returns consecutive keys and groups from the iterable. The key is a function computing a key value for each element. If not specified or is None, key defaults to an identity function and returns the element unchanged. Generally, the iterable needs to already be sorted on the same key function.


In [31]:
for k, g in groupby('AAAABBBCCDAABBB'):
    print k,list(g)


A ['A', 'A', 'A', 'A']
B ['B', 'B', 'B']
C ['C', 'C']
D ['D']
A ['A', 'A']
B ['B', 'B', 'B']

9 迭代器函数

  • ifilter(predicate, iterable)
  • ifilter(predicate, iterable)
  • imap(function, *iterables)
  • islice(iterable, stop)
  • izip(*iterables)
  • izip_longest(*iterables[, fillvalue])

10 product(*iterables[, repeat])

Cartesian product of input iterables.
((x,y) for x in A for y in B).


In [33]:
for value in product('gau','fung'):
    print value


('g', 'f')
('g', 'u')
('g', 'n')
('g', 'g')
('a', 'f')
('a', 'u')
('a', 'n')
('a', 'g')
('u', 'f')
('u', 'u')
('u', 'n')
('u', 'g')

In [35]:
for value in product(range(2), repeat=3):
    print value


(0, 0, 0)
(0, 0, 1)
(0, 1, 0)
(0, 1, 1)
(1, 0, 0)
(1, 0, 1)
(1, 1, 0)
(1, 1, 1)

11 repeat(object[, times])

Make an iterator that returns object over and over again. Runs indefinitely unless the times argument is specified.


In [39]:
for value in repeat('gau', 4):
    print value,


gau gau gau gau

12 takewhile(predicate, iterable)

Make an iterator that returns elements from the iterable as long as the predicate is true.


In [41]:
for value in takewhile(lambda x: x<5, [1,4,6,4,1]):
    print value,


1 4

13 tee(iterable[, n=2])

Return n independent iterators from a single iterable.