blob: 0d74c598e38fd6f99258fafdff7a240917812600 [file] [log] [blame]
Georg Brandl116aa622007-08-15 14:28:22 +00001
2:mod:`itertools` --- Functions creating iterators for efficient looping
3=======================================================================
4
5.. module:: itertools
6 :synopsis: Functions creating iterators for efficient looping.
7.. moduleauthor:: Raymond Hettinger <python@rcn.com>
8.. sectionauthor:: Raymond Hettinger <python@rcn.com>
9
10
Georg Brandl9afde1c2007-11-01 20:32:30 +000011This module implements a number of :term:`iterator` building blocks inspired by
Georg Brandl116aa622007-08-15 14:28:22 +000012constructs from the Haskell and SML programming languages. Each has been recast
13in a form suitable for Python.
14
15The module standardizes a core set of fast, memory efficient tools that are
16useful by themselves or in combination. Standardization helps avoid the
17readability and reliability problems which arise when many different individuals
18create their own slightly varying implementations, each with their own quirks
19and naming conventions.
20
21The tools are designed to combine readily with one another. This makes it easy
22to construct more specialized tools succinctly and efficiently in pure Python.
23
24For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
25sequence ``f(0), f(1), ...``. This toolbox provides :func:`imap` and
26:func:`count` which can be combined to form ``imap(f, count())`` and produce an
27equivalent result.
28
29Likewise, the functional tools are designed to work well with the high-speed
30functions provided by the :mod:`operator` module.
31
32The module author welcomes suggestions for other basic building blocks to be
33added to future versions of the module.
34
35Whether cast in pure python form or compiled code, tools that use iterators are
36more memory efficient (and faster) than their list based counterparts. Adopting
37the principles of just-in-time manufacturing, they create data when and where
38needed instead of consuming memory with the computer equivalent of "inventory".
39
40The performance advantage of iterators becomes more acute as the number of
41elements increases -- at some point, lists grow large enough to severely impact
42memory cache performance and start running slowly.
43
44
45.. seealso::
46
47 The Standard ML Basis Library, `The Standard ML Basis Library
48 <http://www.standardml.org/Basis/>`_.
49
50 Haskell, A Purely Functional Language, `Definition of Haskell and the Standard
51 Libraries <http://www.haskell.org/definition/>`_.
52
53
54.. _itertools-functions:
55
56Itertool functions
57------------------
58
59The following module functions all construct and return iterators. Some provide
60streams of infinite length, so they should only be accessed by functions or
61loops that truncate the stream.
62
63
64.. function:: chain(*iterables)
65
66 Make an iterator that returns elements from the first iterable until it is
67 exhausted, then proceeds to the next iterable, until all of the iterables are
68 exhausted. Used for treating consecutive sequences as a single sequence.
69 Equivalent to::
70
71 def chain(*iterables):
72 for it in iterables:
73 for element in it:
74 yield element
75
76
Christian Heimes70e7ea22008-02-28 20:02:27 +000077.. function:: itertools.chain.from_iterable(iterable)
78
79 Alternate constructor for :func:`chain`. Gets chained inputs from a
80 single iterable argument that is evaluated lazily. Equivalent to::
81
82 @classmethod
83 def from_iterable(iterables):
84 for it in iterables:
85 for element in it:
86 yield element
87
88 .. versionadded:: 2.6
89
Christian Heimes78644762008-03-04 23:39:23 +000090
Christian Heimes836baa52008-02-26 08:18:30 +000091.. function:: combinations(iterable, r)
92
93 Return successive *r* length combinations of elements in the *iterable*.
94
Christian Heimes70e7ea22008-02-28 20:02:27 +000095 Combinations are emitted in lexicographic sort order. So, if the
Christian Heimes836baa52008-02-26 08:18:30 +000096 input *iterable* is sorted, the combination tuples will be produced
97 in sorted order.
98
99 Elements are treated as unique based on their position, not on their
100 value. So if the input elements are unique, there will be no repeat
Christian Heimes70e7ea22008-02-28 20:02:27 +0000101 values in each combination.
Christian Heimes836baa52008-02-26 08:18:30 +0000102
103 Each result tuple is ordered to match the input order. So, every
104 combination is a subsequence of the input *iterable*.
105
Christian Heimes836baa52008-02-26 08:18:30 +0000106 Equivalent to::
107
108 def combinations(iterable, r):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000109 'combinations(range(4), 3) --> (0,1,2) (0,1,3) (0,2,3) (1,2,3)'
Christian Heimes836baa52008-02-26 08:18:30 +0000110 pool = tuple(iterable)
Christian Heimes380f7f22008-02-28 11:19:05 +0000111 n = len(pool)
Christian Heimesb558a2e2008-03-02 22:46:37 +0000112 indices = range(r)
113 yield tuple(pool[i] for i in indices)
Christian Heimes380f7f22008-02-28 11:19:05 +0000114 while 1:
115 for i in reversed(range(r)):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000116 if indices[i] != i + n - r:
Christian Heimes836baa52008-02-26 08:18:30 +0000117 break
Christian Heimes380f7f22008-02-28 11:19:05 +0000118 else:
119 return
Christian Heimesb558a2e2008-03-02 22:46:37 +0000120 indices[i] += 1
Christian Heimes380f7f22008-02-28 11:19:05 +0000121 for j in range(i+1, r):
Christian Heimesb558a2e2008-03-02 22:46:37 +0000122 indices[j] = indices[j-1] + 1
123 yield tuple(pool[i] for i in indices)
Christian Heimes836baa52008-02-26 08:18:30 +0000124
Christian Heimes78644762008-03-04 23:39:23 +0000125 The code for :func:`combinations` can be also expressed as a subsequence
126 of :func:`permutations` after filtering entries where the elements are not
127 in sorted order (according to their position in the input pool)::
128
129 def combinations(iterable, r):
130 pool = tuple(iterable)
131 n = len(pool)
132 for indices in permutations(range(n), r):
133 if sorted(indices) == list(indices):
134 yield tuple(pool[i] for i in indices)
135
Christian Heimes836baa52008-02-26 08:18:30 +0000136 .. versionadded:: 2.6
137
Georg Brandl116aa622007-08-15 14:28:22 +0000138.. function:: count([n])
139
140 Make an iterator that returns consecutive integers starting with *n*. If not
Georg Brandl9afde1c2007-11-01 20:32:30 +0000141 specified *n* defaults to zero. Often used as an argument to :func:`imap` to
142 generate consecutive data points. Also, used with :func:`izip` to add sequence
143 numbers. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000144
145 def count(n=0):
146 while True:
147 yield n
148 n += 1
149
Georg Brandl116aa622007-08-15 14:28:22 +0000150
151.. function:: cycle(iterable)
152
153 Make an iterator returning elements from the iterable and saving a copy of each.
154 When the iterable is exhausted, return elements from the saved copy. Repeats
155 indefinitely. Equivalent to::
156
157 def cycle(iterable):
158 saved = []
159 for element in iterable:
160 yield element
161 saved.append(element)
162 while saved:
163 for element in saved:
164 yield element
165
166 Note, this member of the toolkit may require significant auxiliary storage
167 (depending on the length of the iterable).
168
169
170.. function:: dropwhile(predicate, iterable)
171
172 Make an iterator that drops elements from the iterable as long as the predicate
173 is true; afterwards, returns every element. Note, the iterator does not produce
174 *any* output until the predicate first becomes false, so it may have a lengthy
175 start-up time. Equivalent to::
176
177 def dropwhile(predicate, iterable):
178 iterable = iter(iterable)
179 for x in iterable:
180 if not predicate(x):
181 yield x
182 break
183 for x in iterable:
184 yield x
185
186
187.. function:: groupby(iterable[, key])
188
189 Make an iterator that returns consecutive keys and groups from the *iterable*.
190 The *key* is a function computing a key value for each element. If not
191 specified or is ``None``, *key* defaults to an identity function and returns
192 the element unchanged. Generally, the iterable needs to already be sorted on
193 the same key function.
194
195 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
196 generates a break or new group every time the value of the key function changes
197 (which is why it is usually necessary to have sorted the data using the same key
198 function). That behavior differs from SQL's GROUP BY which aggregates common
199 elements regardless of their input order.
200
201 The returned group is itself an iterator that shares the underlying iterable
202 with :func:`groupby`. Because the source is shared, when the :func:`groupby`
203 object is advanced, the previous group is no longer visible. So, if that data
204 is needed later, it should be stored as a list::
205
206 groups = []
207 uniquekeys = []
208 data = sorted(data, key=keyfunc)
209 for k, g in groupby(data, keyfunc):
210 groups.append(list(g)) # Store group iterator as a list
211 uniquekeys.append(k)
212
213 :func:`groupby` is equivalent to::
214
215 class groupby(object):
216 def __init__(self, iterable, key=None):
217 if key is None:
218 key = lambda x: x
219 self.keyfunc = key
220 self.it = iter(iterable)
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000221 self.tgtkey = self.currkey = self.currvalue = object()
Georg Brandl116aa622007-08-15 14:28:22 +0000222 def __iter__(self):
223 return self
224 def __next__(self):
225 while self.currkey == self.tgtkey:
226 self.currvalue = next(self.it) # Exit on StopIteration
227 self.currkey = self.keyfunc(self.currvalue)
228 self.tgtkey = self.currkey
229 return (self.currkey, self._grouper(self.tgtkey))
230 def _grouper(self, tgtkey):
231 while self.currkey == tgtkey:
232 yield self.currvalue
233 self.currvalue = next(self.it) # Exit on StopIteration
234 self.currkey = self.keyfunc(self.currvalue)
235
Georg Brandl116aa622007-08-15 14:28:22 +0000236
237.. function:: ifilter(predicate, iterable)
238
239 Make an iterator that filters elements from iterable returning only those for
240 which the predicate is ``True``. If *predicate* is ``None``, return the items
Georg Brandlf6945182008-02-01 11:56:49 +0000241 that are true. This function is the same as the built-in :func:`filter`
242 function. Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000243
244 def ifilter(predicate, iterable):
245 if predicate is None:
246 predicate = bool
247 for x in iterable:
248 if predicate(x):
249 yield x
250
251
252.. function:: ifilterfalse(predicate, iterable)
253
254 Make an iterator that filters elements from iterable returning only those for
255 which the predicate is ``False``. If *predicate* is ``None``, return the items
256 that are false. Equivalent to::
257
258 def ifilterfalse(predicate, iterable):
259 if predicate is None:
260 predicate = bool
261 for x in iterable:
262 if not predicate(x):
263 yield x
264
265
266.. function:: imap(function, *iterables)
267
268 Make an iterator that computes the function using arguments from each of the
Georg Brandlf6945182008-02-01 11:56:49 +0000269 iterables. This function is the same as the built-in :func:`map` function.
270 Equivalent to::
Georg Brandl116aa622007-08-15 14:28:22 +0000271
272 def imap(function, *iterables):
Raymond Hettinger1dfde1d2008-01-22 23:25:35 +0000273 iterables = [iter(it) for it in iterables)
Georg Brandl116aa622007-08-15 14:28:22 +0000274 while True:
Raymond Hettinger1dfde1d2008-01-22 23:25:35 +0000275 args = [next(it) for it in iterables]
Christian Heimes1af737c2008-01-23 08:24:23 +0000276 if function is None:
277 yield tuple(args)
278 else:
279 yield function(*args)
Georg Brandl116aa622007-08-15 14:28:22 +0000280
281
282.. function:: islice(iterable, [start,] stop [, step])
283
284 Make an iterator that returns selected elements from the iterable. If *start* is
285 non-zero, then elements from the iterable are skipped until start is reached.
286 Afterward, elements are returned consecutively unless *step* is set higher than
287 one which results in items being skipped. If *stop* is ``None``, then iteration
288 continues until the iterator is exhausted, if at all; otherwise, it stops at the
289 specified position. Unlike regular slicing, :func:`islice` does not support
290 negative values for *start*, *stop*, or *step*. Can be used to extract related
291 fields from data where the internal structure has been flattened (for example, a
292 multi-line report may list a name field on every third line). Equivalent to::
293
294 def islice(iterable, *args):
295 s = slice(*args)
Georg Brandlf6945182008-02-01 11:56:49 +0000296 it = range(s.start or 0, s.stop or sys.maxsize, s.step or 1)
Georg Brandl116aa622007-08-15 14:28:22 +0000297 nexti = next(it)
298 for i, element in enumerate(iterable):
299 if i == nexti:
300 yield element
301 nexti = next(it)
302
303 If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
304 then the step defaults to one.
305
Georg Brandl116aa622007-08-15 14:28:22 +0000306
307.. function:: izip(*iterables)
308
309 Make an iterator that aggregates elements from each of the iterables. Like
310 :func:`zip` except that it returns an iterator instead of a list. Used for
311 lock-step iteration over several iterables at a time. Equivalent to::
312
313 def izip(*iterables):
314 iterables = map(iter, iterables)
315 while iterables:
316 result = [next(it) for it in iterables]
317 yield tuple(result)
318
Georg Brandl55ac8f02007-09-01 13:51:09 +0000319 When no iterables are specified, return a zero length iterator.
Georg Brandl116aa622007-08-15 14:28:22 +0000320
Christian Heimes1af737c2008-01-23 08:24:23 +0000321 The left-to-right evaluation order of the iterables is guaranteed. This
322 makes possible an idiom for clustering a data series into n-length groups
323 using ``izip(*[iter(s)]*n)``.
Georg Brandl116aa622007-08-15 14:28:22 +0000324
Christian Heimes1af737c2008-01-23 08:24:23 +0000325 :func:`izip` should only be used with unequal length inputs when you don't
326 care about trailing, unmatched values from the longer iterables. If those
327 values are important, use :func:`izip_longest` instead.
Georg Brandl116aa622007-08-15 14:28:22 +0000328
329
330.. function:: izip_longest(*iterables[, fillvalue])
331
332 Make an iterator that aggregates elements from each of the iterables. If the
333 iterables are of uneven length, missing values are filled-in with *fillvalue*.
334 Iteration continues until the longest iterable is exhausted. Equivalent to::
335
336 def izip_longest(*args, **kwds):
337 fillvalue = kwds.get('fillvalue')
338 def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
339 yield counter() # yields the fillvalue, or raises IndexError
340 fillers = repeat(fillvalue)
341 iters = [chain(it, sentinel(), fillers) for it in args]
342 try:
343 for tup in izip(*iters):
344 yield tup
345 except IndexError:
346 pass
347
348 If one of the iterables is potentially infinite, then the :func:`izip_longest`
349 function should be wrapped with something that limits the number of calls (for
350 example :func:`islice` or :func:`takewhile`).
351
Georg Brandl116aa622007-08-15 14:28:22 +0000352
Christian Heimes70e7ea22008-02-28 20:02:27 +0000353.. function:: permutations(iterable[, r])
354
355 Return successive *r* length permutations of elements in the *iterable*.
356
357 If *r* is not specified or is ``None``, then *r* defaults to the length
358 of the *iterable* and all possible full-length permutations
359 are generated.
360
361 Permutations are emitted in lexicographic sort order. So, if the
362 input *iterable* is sorted, the permutation tuples will be produced
363 in sorted order.
364
365 Elements are treated as unique based on their position, not on their
366 value. So if the input elements are unique, there will be no repeat
367 values in each permutation.
368
Christian Heimesb558a2e2008-03-02 22:46:37 +0000369 Equivalent to::
370
371 def permutations(iterable, r=None):
372 'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
373 pool = tuple(iterable)
374 n = len(pool)
375 r = n if r is None else r
376 indices = range(n)
377 cycles = range(n-r+1, n+1)[::-1]
378 yield tuple(pool[i] for i in indices[:r])
379 while n:
380 for i in reversed(range(r)):
381 cycles[i] -= 1
382 if cycles[i] == 0:
383 indices[i:] = indices[i+1:] + indices[i:i+1]
384 cycles[i] = n - i
385 else:
386 j = cycles[i]
387 indices[i], indices[-j] = indices[-j], indices[i]
388 yield tuple(pool[i] for i in indices[:r])
389 break
390 else:
391 return
Christian Heimes70e7ea22008-02-28 20:02:27 +0000392
Christian Heimes78644762008-03-04 23:39:23 +0000393 The code for :func:`permutations` can be also expressed as a subsequence of
394 :func:`product`, filtered to exclude entries with repeated elements (those
395 from the same position in the input pool)::
396
397 def permutations(iterable, r=None):
398 pool = tuple(iterable)
399 n = len(pool)
400 r = n if r is None else r
401 for indices in product(range(n), repeat=r):
402 if len(set(indices)) == r:
403 yield tuple(pool[i] for i in indices)
404
Christian Heimes70e7ea22008-02-28 20:02:27 +0000405 .. versionadded:: 2.6
406
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000407.. function:: product(*iterables[, repeat])
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000408
409 Cartesian product of input iterables.
410
411 Equivalent to nested for-loops in a generator expression. For example,
412 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
413
414 The leftmost iterators are in the outermost for-loop, so the output tuples
Christian Heimes78644762008-03-04 23:39:23 +0000415 cycle like an odometer (with the rightmost element changing on every
416 iteration). This results in a lexicographic ordering so that if the
417 inputs iterables are sorted, the product tuples are emitted
Christian Heimes836baa52008-02-26 08:18:30 +0000418 in sorted order.
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000419
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000420 To compute the product of an iterable with itself, specify the number of
421 repetitions with the optional *repeat* keyword argument. For example,
422 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
423
Christian Heimes78644762008-03-04 23:39:23 +0000424 This function is equivalent to the following code, except that the
425 actual implementation does not build up intermediate results in memory::
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000426
Christian Heimes9e7f1d22008-02-28 12:27:11 +0000427 def product(*args, **kwds):
428 pools = map(tuple, args) * kwds.get('repeat', 1)
Christian Heimes78644762008-03-04 23:39:23 +0000429 result = [[]]
430 for pool in pools:
431 result = [x+[y] for x in result for y in pool]
432 for prod in result:
433 yield tuple(prod)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000434
435
Georg Brandl116aa622007-08-15 14:28:22 +0000436.. function:: repeat(object[, times])
437
438 Make an iterator that returns *object* over and over again. Runs indefinitely
439 unless the *times* argument is specified. Used as argument to :func:`imap` for
440 invariant parameters to the called function. Also used with :func:`izip` to
441 create an invariant part of a tuple record. Equivalent to::
442
443 def repeat(object, times=None):
444 if times is None:
445 while True:
446 yield object
447 else:
448 for i in range(times):
449 yield object
450
451
452.. function:: starmap(function, iterable)
453
Christian Heimes679db4a2008-01-18 09:56:22 +0000454 Make an iterator that computes the function using arguments obtained from
Georg Brandl116aa622007-08-15 14:28:22 +0000455 the iterable. Used instead of :func:`imap` when argument parameters are already
456 grouped in tuples from a single iterable (the data has been "pre-zipped"). The
457 difference between :func:`imap` and :func:`starmap` parallels the distinction
458 between ``function(a,b)`` and ``function(*c)``. Equivalent to::
459
460 def starmap(function, iterable):
Christian Heimes679db4a2008-01-18 09:56:22 +0000461 for args in iterable:
462 yield function(*args)
463
464 .. versionchanged:: 2.6
465 Previously, :func:`starmap` required the function arguments to be tuples.
466 Now, any iterable is allowed.
Georg Brandl116aa622007-08-15 14:28:22 +0000467
468
469.. function:: takewhile(predicate, iterable)
470
471 Make an iterator that returns elements from the iterable as long as the
472 predicate is true. Equivalent to::
473
474 def takewhile(predicate, iterable):
475 for x in iterable:
476 if predicate(x):
477 yield x
478 else:
479 break
480
481
482.. function:: tee(iterable[, n=2])
483
484 Return *n* independent iterators from a single iterable. The case where ``n==2``
485 is equivalent to::
486
487 def tee(iterable):
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000488 def gen(next, data={}):
Georg Brandl116aa622007-08-15 14:28:22 +0000489 for i in count():
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000490 if i in data:
491 yield data.pop(i)
Georg Brandl116aa622007-08-15 14:28:22 +0000492 else:
Christian Heimes5b5e81c2007-12-31 16:14:33 +0000493 data[i] = next()
494 yield data[i]
Georg Brandl116aa622007-08-15 14:28:22 +0000495 it = iter(iterable)
496 return (gen(it.__next__), gen(it.__next__))
497
498 Note, once :func:`tee` has made a split, the original *iterable* should not be
499 used anywhere else; otherwise, the *iterable* could get advanced without the tee
500 objects being informed.
501
502 Note, this member of the toolkit may require significant auxiliary storage
503 (depending on how much temporary data needs to be stored). In general, if one
504 iterator is going to use most or all of the data before the other iterator, it
505 is faster to use :func:`list` instead of :func:`tee`.
506
Georg Brandl116aa622007-08-15 14:28:22 +0000507
508.. _itertools-example:
509
510Examples
511--------
512
513The following examples show common uses for each tool and demonstrate ways they
514can be combined. ::
515
516 >>> amounts = [120.15, 764.05, 823.14]
517 >>> for checknum, amount in izip(count(1200), amounts):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000518 ... print('Check %d is for $%.2f' % (checknum, amount))
Georg Brandl116aa622007-08-15 14:28:22 +0000519 ...
520 Check 1200 is for $120.15
521 Check 1201 is for $764.05
522 Check 1202 is for $823.14
523
524 >>> import operator
525 >>> for cube in imap(operator.pow, range(1,5), repeat(3)):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000526 ... print(cube)
Georg Brandl116aa622007-08-15 14:28:22 +0000527 ...
528 1
529 8
530 27
531 64
532
533 >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura',
534 ... '', 'martin', '', 'walter', '', 'mark']
535 >>> for name in islice(reportlines, 3, None, 2):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000536 ... print(name.title())
Georg Brandl116aa622007-08-15 14:28:22 +0000537 ...
538 Alex
539 Laura
540 Martin
541 Walter
542 Mark
543
544 # Show a dictionary sorted and grouped by value
545 >>> from operator import itemgetter
546 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
Fred Drake2e748782007-09-04 17:33:11 +0000547 >>> di = sorted(d.items(), key=itemgetter(1))
Georg Brandl116aa622007-08-15 14:28:22 +0000548 >>> for k, g in groupby(di, key=itemgetter(1)):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000549 ... print(k, map(itemgetter(0), g))
Georg Brandl116aa622007-08-15 14:28:22 +0000550 ...
551 1 ['a', 'c', 'e']
552 2 ['b', 'd', 'f']
553 3 ['g']
554
555 # Find runs of consecutive numbers using groupby. The key to the solution
556 # is differencing with a range so that consecutive numbers all appear in
557 # same group.
558 >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
559 >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]):
Georg Brandl6911e3c2007-09-04 07:15:32 +0000560 ... print(map(operator.itemgetter(1), g))
Georg Brandl116aa622007-08-15 14:28:22 +0000561 ...
562 [1]
563 [4, 5, 6]
564 [10]
565 [15, 16, 17, 18]
566 [22]
567 [25, 26, 27, 28]
568
569
570
571.. _itertools-recipes:
572
573Recipes
574-------
575
576This section shows recipes for creating an extended toolset using the existing
577itertools as building blocks.
578
579The extended tools offer the same high performance as the underlying toolset.
580The superior memory performance is kept by processing elements one at a time
581rather than bringing the whole iterable into memory all at once. Code volume is
582kept small by linking the tools together in a functional style which helps
583eliminate temporary variables. High speed is retained by preferring
Georg Brandl9afde1c2007-11-01 20:32:30 +0000584"vectorized" building blocks over the use of for-loops and :term:`generator`\s
585which incur interpreter overhead. ::
Georg Brandl116aa622007-08-15 14:28:22 +0000586
587 def take(n, seq):
588 return list(islice(seq, n))
589
590 def enumerate(iterable):
591 return izip(count(), iterable)
592
593 def tabulate(function):
594 "Return function(0), function(1), ..."
595 return imap(function, count())
596
Georg Brandl116aa622007-08-15 14:28:22 +0000597 def nth(iterable, n):
598 "Returns the nth item or raise StopIteration"
599 return islice(iterable, n, None).next()
600
601 def all(seq, pred=None):
602 "Returns True if pred(x) is true for every element in the iterable"
603 for elem in ifilterfalse(pred, seq):
604 return False
605 return True
606
607 def any(seq, pred=None):
608 "Returns True if pred(x) is true for at least one element in the iterable"
609 for elem in ifilter(pred, seq):
610 return True
611 return False
612
613 def no(seq, pred=None):
614 "Returns True if pred(x) is false for every element in the iterable"
615 for elem in ifilter(pred, seq):
616 return False
617 return True
618
619 def quantify(seq, pred=None):
620 "Count how many times the predicate is true in the sequence"
621 return sum(imap(pred, seq))
622
623 def padnone(seq):
624 """Returns the sequence elements and then returns None indefinitely.
625
626 Useful for emulating the behavior of the built-in map() function.
627 """
628 return chain(seq, repeat(None))
629
630 def ncycles(seq, n):
631 "Returns the sequence elements n times"
Christian Heimes70e7ea22008-02-28 20:02:27 +0000632 return chain.from_iterable(repeat(seq, n))
Georg Brandl116aa622007-08-15 14:28:22 +0000633
634 def dotproduct(vec1, vec2):
635 return sum(imap(operator.mul, vec1, vec2))
636
637 def flatten(listOfLists):
Christian Heimes70e7ea22008-02-28 20:02:27 +0000638 return list(chain.from_iterable(listOfLists))
Georg Brandl116aa622007-08-15 14:28:22 +0000639
640 def repeatfunc(func, times=None, *args):
641 """Repeat calls to func with specified arguments.
642
643 Example: repeatfunc(random.random)
644 """
645 if times is None:
646 return starmap(func, repeat(args))
Christian Heimes70e7ea22008-02-28 20:02:27 +0000647 return starmap(func, repeat(args, times))
Georg Brandl116aa622007-08-15 14:28:22 +0000648
649 def pairwise(iterable):
650 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
651 a, b = tee(iterable)
652 next(b, None)
653 return izip(a, b)
654
655 def grouper(n, iterable, padvalue=None):
656 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
657 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
658
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000659 def roundrobin(*iterables):
660 "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
Christian Heimes70e7ea22008-02-28 20:02:27 +0000661 # Recipe credited to George Sakkis
Christian Heimes7b3ce6a2008-01-31 14:31:45 +0000662 pending = len(iterables)
663 nexts = cycle(iter(it).next for it in iterables)
664 while pending:
665 try:
666 for next in nexts:
667 yield next()
668 except StopIteration:
669 pending -= 1
670 nexts = cycle(islice(nexts, pending))
Georg Brandl116aa622007-08-15 14:28:22 +0000671
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000672 def powerset(iterable):
Christian Heimes70e7ea22008-02-28 20:02:27 +0000673 "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])"
674 # Recipe credited to Eric Raymond
675 pairs = [(2**i, x) for i, x in enumerate(iterable)]
676 for n in xrange(2**len(pairs)):
677 yield set(x for m, x in pairs if m&n)
Christian Heimes90c3d9b2008-02-23 13:18:03 +0000678