blob: 3f2abdc661f1e75fca31a87344d0fe584b0ebbcb [file] [log] [blame]
Georg Brandl8ec7f652007-08-15 14:28:01 +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
11.. versionadded:: 2.3
12
Georg Brandle7a09902007-10-21 12:10:28 +000013This module implements a number of :term:`iterator` building blocks inspired by
Georg Brandl8ec7f652007-08-15 14:28:01 +000014constructs from the Haskell and SML programming languages. Each has been recast
15in a form suitable for Python.
16
17The module standardizes a core set of fast, memory efficient tools that are
18useful by themselves or in combination. Standardization helps avoid the
19readability and reliability problems which arise when many different individuals
20create their own slightly varying implementations, each with their own quirks
21and naming conventions.
22
23The tools are designed to combine readily with one another. This makes it easy
24to construct more specialized tools succinctly and efficiently in pure Python.
25
26For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a
27sequence ``f(0), f(1), ...``. This toolbox provides :func:`imap` and
28:func:`count` which can be combined to form ``imap(f, count())`` and produce an
29equivalent result.
30
31Likewise, the functional tools are designed to work well with the high-speed
32functions provided by the :mod:`operator` module.
33
34The module author welcomes suggestions for other basic building blocks to be
35added to future versions of the module.
36
37Whether cast in pure python form or compiled code, tools that use iterators are
38more memory efficient (and faster) than their list based counterparts. Adopting
39the principles of just-in-time manufacturing, they create data when and where
40needed instead of consuming memory with the computer equivalent of "inventory".
41
42The performance advantage of iterators becomes more acute as the number of
43elements increases -- at some point, lists grow large enough to severely impact
44memory cache performance and start running slowly.
45
46
47.. seealso::
48
49 The Standard ML Basis Library, `The Standard ML Basis Library
50 <http://www.standardml.org/Basis/>`_.
51
52 Haskell, A Purely Functional Language, `Definition of Haskell and the Standard
53 Libraries <http://www.haskell.org/definition/>`_.
54
55
56.. _itertools-functions:
57
58Itertool functions
59------------------
60
61The following module functions all construct and return iterators. Some provide
62streams of infinite length, so they should only be accessed by functions or
63loops that truncate the stream.
64
65
66.. function:: chain(*iterables)
67
68 Make an iterator that returns elements from the first iterable until it is
69 exhausted, then proceeds to the next iterable, until all of the iterables are
70 exhausted. Used for treating consecutive sequences as a single sequence.
71 Equivalent to::
72
73 def chain(*iterables):
74 for it in iterables:
75 for element in it:
76 yield element
77
78
Raymond Hettinger330958e62008-02-28 19:41:24 +000079.. function:: itertools.chain.from_iterable(iterable)
80
81 Alternate constructor for :func:`chain`. Gets chained inputs from a
82 single iterable argument that is evaluated lazily. Equivalent to::
83
84 @classmethod
85 def from_iterable(iterables):
86 for it in iterables:
87 for element in it:
88 yield element
89
90 .. versionadded:: 2.6
91
Raymond Hettingerd553d852008-03-04 04:17:08 +000092
Raymond Hettinger3fa41d52008-02-26 02:46:54 +000093.. function:: combinations(iterable, r)
94
95 Return successive *r* length combinations of elements in the *iterable*.
96
Raymond Hettinger330958e62008-02-28 19:41:24 +000097 Combinations are emitted in lexicographic sort order. So, if the
Raymond Hettinger3fa41d52008-02-26 02:46:54 +000098 input *iterable* is sorted, the combination tuples will be produced
99 in sorted order.
100
101 Elements are treated as unique based on their position, not on their
102 value. So if the input elements are unique, there will be no repeat
Raymond Hettinger330958e62008-02-28 19:41:24 +0000103 values in each combination.
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000104
105 Each result tuple is ordered to match the input order. So, every
106 combination is a subsequence of the input *iterable*.
107
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000108 Equivalent to::
109
110 def combinations(iterable, r):
Raymond Hettingerf287f172008-03-02 10:59:31 +0000111 'combinations(range(4), 3) --> (0,1,2) (0,1,3) (0,2,3) (1,2,3)'
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000112 pool = tuple(iterable)
Raymond Hettinger93e804d2008-02-26 23:40:50 +0000113 n = len(pool)
Raymond Hettingerf287f172008-03-02 10:59:31 +0000114 indices = range(r)
115 yield tuple(pool[i] for i in indices)
Raymond Hettinger93e804d2008-02-26 23:40:50 +0000116 while 1:
117 for i in reversed(range(r)):
Raymond Hettingerf287f172008-03-02 10:59:31 +0000118 if indices[i] != i + n - r:
Raymond Hettingerc1052892008-02-27 01:44:34 +0000119 break
Raymond Hettinger93e804d2008-02-26 23:40:50 +0000120 else:
121 return
Raymond Hettingerf287f172008-03-02 10:59:31 +0000122 indices[i] += 1
Raymond Hettingerc1052892008-02-27 01:44:34 +0000123 for j in range(i+1, r):
Raymond Hettingerf287f172008-03-02 10:59:31 +0000124 indices[j] = indices[j-1] + 1
125 yield tuple(pool[i] for i in indices)
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000126
Raymond Hettingerd553d852008-03-04 04:17:08 +0000127 The code for :func:`combinations` can be also expressed as a subsequence
128 of :func:`permutations` after filtering entries where the elements are not
129 in sorted order (according to their position in the input pool)::
130
131 def combinations(iterable, r):
132 pool = tuple(iterable)
133 n = len(pool)
134 for indices in permutations(range(n), r):
135 if sorted(indices) == list(indices):
136 yield tuple(pool[i] for i in indices)
137
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000138 .. versionadded:: 2.6
139
Georg Brandl8ec7f652007-08-15 14:28:01 +0000140.. function:: count([n])
141
142 Make an iterator that returns consecutive integers starting with *n*. If not
Raymond Hettinger50e90e22007-10-04 00:20:27 +0000143 specified *n* defaults to zero. Often used as an argument to :func:`imap` to
144 generate consecutive data points. Also, used with :func:`izip` to add sequence
145 numbers. Equivalent to::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000146
147 def count(n=0):
148 while True:
149 yield n
150 n += 1
151
Georg Brandl8ec7f652007-08-15 14:28:01 +0000152
153.. function:: cycle(iterable)
154
155 Make an iterator returning elements from the iterable and saving a copy of each.
156 When the iterable is exhausted, return elements from the saved copy. Repeats
157 indefinitely. Equivalent to::
158
159 def cycle(iterable):
160 saved = []
161 for element in iterable:
162 yield element
163 saved.append(element)
164 while saved:
165 for element in saved:
166 yield element
167
168 Note, this member of the toolkit may require significant auxiliary storage
169 (depending on the length of the iterable).
170
171
172.. function:: dropwhile(predicate, iterable)
173
174 Make an iterator that drops elements from the iterable as long as the predicate
175 is true; afterwards, returns every element. Note, the iterator does not produce
176 *any* output until the predicate first becomes false, so it may have a lengthy
177 start-up time. Equivalent to::
178
179 def dropwhile(predicate, iterable):
180 iterable = iter(iterable)
181 for x in iterable:
182 if not predicate(x):
183 yield x
184 break
185 for x in iterable:
186 yield x
187
188
189.. function:: groupby(iterable[, key])
190
191 Make an iterator that returns consecutive keys and groups from the *iterable*.
192 The *key* is a function computing a key value for each element. If not
193 specified or is ``None``, *key* defaults to an identity function and returns
194 the element unchanged. Generally, the iterable needs to already be sorted on
195 the same key function.
196
197 The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It
198 generates a break or new group every time the value of the key function changes
199 (which is why it is usually necessary to have sorted the data using the same key
200 function). That behavior differs from SQL's GROUP BY which aggregates common
201 elements regardless of their input order.
202
203 The returned group is itself an iterator that shares the underlying iterable
204 with :func:`groupby`. Because the source is shared, when the :func:`groupby`
205 object is advanced, the previous group is no longer visible. So, if that data
206 is needed later, it should be stored as a list::
207
208 groups = []
209 uniquekeys = []
210 data = sorted(data, key=keyfunc)
211 for k, g in groupby(data, keyfunc):
212 groups.append(list(g)) # Store group iterator as a list
213 uniquekeys.append(k)
214
215 :func:`groupby` is equivalent to::
216
217 class groupby(object):
218 def __init__(self, iterable, key=None):
219 if key is None:
220 key = lambda x: x
221 self.keyfunc = key
222 self.it = iter(iterable)
Raymond Hettinger81a885a2007-12-29 22:16:24 +0000223 self.tgtkey = self.currkey = self.currvalue = object()
Georg Brandl8ec7f652007-08-15 14:28:01 +0000224 def __iter__(self):
225 return self
226 def next(self):
227 while self.currkey == self.tgtkey:
228 self.currvalue = self.it.next() # Exit on StopIteration
229 self.currkey = self.keyfunc(self.currvalue)
230 self.tgtkey = self.currkey
231 return (self.currkey, self._grouper(self.tgtkey))
232 def _grouper(self, tgtkey):
233 while self.currkey == tgtkey:
234 yield self.currvalue
235 self.currvalue = self.it.next() # Exit on StopIteration
236 self.currkey = self.keyfunc(self.currvalue)
237
238 .. versionadded:: 2.4
239
240
241.. function:: ifilter(predicate, iterable)
242
243 Make an iterator that filters elements from iterable returning only those for
244 which the predicate is ``True``. If *predicate* is ``None``, return the items
245 that are true. Equivalent to::
246
247 def ifilter(predicate, iterable):
248 if predicate is None:
249 predicate = bool
250 for x in iterable:
251 if predicate(x):
252 yield x
253
254
255.. function:: ifilterfalse(predicate, iterable)
256
257 Make an iterator that filters elements from iterable returning only those for
258 which the predicate is ``False``. If *predicate* is ``None``, return the items
259 that are false. Equivalent to::
260
261 def ifilterfalse(predicate, iterable):
262 if predicate is None:
263 predicate = bool
264 for x in iterable:
265 if not predicate(x):
266 yield x
267
268
269.. function:: imap(function, *iterables)
270
271 Make an iterator that computes the function using arguments from each of the
272 iterables. If *function* is set to ``None``, then :func:`imap` returns the
273 arguments as a tuple. Like :func:`map` but stops when the shortest iterable is
274 exhausted instead of filling in ``None`` for shorter iterables. The reason for
275 the difference is that infinite iterator arguments are typically an error for
276 :func:`map` (because the output is fully evaluated) but represent a common and
277 useful way of supplying arguments to :func:`imap`. Equivalent to::
278
279 def imap(function, *iterables):
280 iterables = map(iter, iterables)
281 while True:
Raymond Hettinger2dec48d2008-01-22 22:09:26 +0000282 args = [it.next() for it in iterables]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000283 if function is None:
284 yield tuple(args)
285 else:
286 yield function(*args)
287
288
289.. function:: islice(iterable, [start,] stop [, step])
290
291 Make an iterator that returns selected elements from the iterable. If *start* is
292 non-zero, then elements from the iterable are skipped until start is reached.
293 Afterward, elements are returned consecutively unless *step* is set higher than
294 one which results in items being skipped. If *stop* is ``None``, then iteration
295 continues until the iterator is exhausted, if at all; otherwise, it stops at the
296 specified position. Unlike regular slicing, :func:`islice` does not support
297 negative values for *start*, *stop*, or *step*. Can be used to extract related
298 fields from data where the internal structure has been flattened (for example, a
299 multi-line report may list a name field on every third line). Equivalent to::
300
301 def islice(iterable, *args):
302 s = slice(*args)
303 it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1))
304 nexti = it.next()
305 for i, element in enumerate(iterable):
306 if i == nexti:
307 yield element
308 nexti = it.next()
309
310 If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
311 then the step defaults to one.
312
313 .. versionchanged:: 2.5
314 accept ``None`` values for default *start* and *step*.
315
316
317.. function:: izip(*iterables)
318
319 Make an iterator that aggregates elements from each of the iterables. Like
320 :func:`zip` except that it returns an iterator instead of a list. Used for
321 lock-step iteration over several iterables at a time. Equivalent to::
322
323 def izip(*iterables):
324 iterables = map(iter, iterables)
325 while iterables:
326 result = [it.next() for it in iterables]
327 yield tuple(result)
328
329 .. versionchanged:: 2.4
330 When no iterables are specified, returns a zero length iterator instead of
331 raising a :exc:`TypeError` exception.
332
Raymond Hettinger48c62932008-01-22 19:51:41 +0000333 The left-to-right evaluation order of the iterables is guaranteed. This
334 makes possible an idiom for clustering a data series into n-length groups
335 using ``izip(*[iter(s)]*n)``.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000336
Raymond Hettinger48c62932008-01-22 19:51:41 +0000337 :func:`izip` should only be used with unequal length inputs when you don't
338 care about trailing, unmatched values from the longer iterables. If those
339 values are important, use :func:`izip_longest` instead.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000340
341
342.. function:: izip_longest(*iterables[, fillvalue])
343
344 Make an iterator that aggregates elements from each of the iterables. If the
345 iterables are of uneven length, missing values are filled-in with *fillvalue*.
346 Iteration continues until the longest iterable is exhausted. Equivalent to::
347
348 def izip_longest(*args, **kwds):
349 fillvalue = kwds.get('fillvalue')
350 def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
351 yield counter() # yields the fillvalue, or raises IndexError
352 fillers = repeat(fillvalue)
353 iters = [chain(it, sentinel(), fillers) for it in args]
354 try:
355 for tup in izip(*iters):
356 yield tup
357 except IndexError:
358 pass
359
360 If one of the iterables is potentially infinite, then the :func:`izip_longest`
361 function should be wrapped with something that limits the number of calls (for
362 example :func:`islice` or :func:`takewhile`).
363
364 .. versionadded:: 2.6
365
Raymond Hettinger330958e62008-02-28 19:41:24 +0000366.. function:: permutations(iterable[, r])
367
368 Return successive *r* length permutations of elements in the *iterable*.
369
370 If *r* is not specified or is ``None``, then *r* defaults to the length
371 of the *iterable* and all possible full-length permutations
372 are generated.
373
374 Permutations are emitted in lexicographic sort order. So, if the
375 input *iterable* is sorted, the permutation tuples will be produced
376 in sorted order.
377
378 Elements are treated as unique based on their position, not on their
379 value. So if the input elements are unique, there will be no repeat
380 values in each permutation.
381
Raymond Hettingerf287f172008-03-02 10:59:31 +0000382 Equivalent to::
383
384 def permutations(iterable, r=None):
385 'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)'
386 pool = tuple(iterable)
387 n = len(pool)
388 r = n if r is None else r
389 indices = range(n)
390 cycles = range(n-r+1, n+1)[::-1]
391 yield tuple(pool[i] for i in indices[:r])
392 while n:
393 for i in reversed(range(r)):
394 cycles[i] -= 1
395 if cycles[i] == 0:
Raymond Hettinger2b7a5c42008-03-02 11:17:51 +0000396 indices[i:] = indices[i+1:] + indices[i:i+1]
Raymond Hettingerf287f172008-03-02 10:59:31 +0000397 cycles[i] = n - i
398 else:
399 j = cycles[i]
400 indices[i], indices[-j] = indices[-j], indices[i]
401 yield tuple(pool[i] for i in indices[:r])
402 break
403 else:
404 return
Raymond Hettinger330958e62008-02-28 19:41:24 +0000405
Raymond Hettingerd553d852008-03-04 04:17:08 +0000406 The code for :func:`permutations` can be also expressed as a subsequence of
407 :func:`product`, filtered to exclude entries with repeated elements (those
408 from the same position in the input pool)::
409
410 def permutations(iterable, r=None):
411 pool = tuple(iterable)
412 n = len(pool)
413 r = n if r is None else r
414 for indices in product(range(n), repeat=r):
415 if len(set(indices)) == r:
416 yield tuple(pool[i] for i in indices)
417
Raymond Hettinger330958e62008-02-28 19:41:24 +0000418 .. versionadded:: 2.6
419
Raymond Hettinger18750ab2008-02-28 09:23:48 +0000420.. function:: product(*iterables[, repeat])
Raymond Hettingerc5705a82008-02-22 19:50:06 +0000421
422 Cartesian product of input iterables.
423
424 Equivalent to nested for-loops in a generator expression. For example,
425 ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
426
427 The leftmost iterators are in the outermost for-loop, so the output tuples
Raymond Hettingerd553d852008-03-04 04:17:08 +0000428 cycle like an odometer (with the rightmost element changing on every
429 iteration). This results in a lexicographic ordering so that if the
430 inputs iterables are sorted, the product tuples are emitted
Raymond Hettinger3fa41d52008-02-26 02:46:54 +0000431 in sorted order.
Raymond Hettingerc5705a82008-02-22 19:50:06 +0000432
Raymond Hettinger18750ab2008-02-28 09:23:48 +0000433 To compute the product of an iterable with itself, specify the number of
434 repetitions with the optional *repeat* keyword argument. For example,
435 ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``.
436
Andrew M. Kuchling684868a2008-03-04 01:47:38 +0000437 This function is equivalent to the following code, except that the
438 actual implementation does not build up intermediate results in memory::
Raymond Hettingerc5705a82008-02-22 19:50:06 +0000439
Raymond Hettinger18750ab2008-02-28 09:23:48 +0000440 def product(*args, **kwds):
441 pools = map(tuple, args) * kwds.get('repeat', 1)
Raymond Hettingerd553d852008-03-04 04:17:08 +0000442 result = [[]]
443 for pool in pools:
444 result = [x+[y] for x in result for y in pool]
445 for prod in result:
446 yield tuple(prod)
Raymond Hettingerc5705a82008-02-22 19:50:06 +0000447
448 .. versionadded:: 2.6
Georg Brandl8ec7f652007-08-15 14:28:01 +0000449
450.. function:: repeat(object[, times])
451
452 Make an iterator that returns *object* over and over again. Runs indefinitely
453 unless the *times* argument is specified. Used as argument to :func:`imap` for
454 invariant parameters to the called function. Also used with :func:`izip` to
455 create an invariant part of a tuple record. Equivalent to::
456
457 def repeat(object, times=None):
458 if times is None:
459 while True:
460 yield object
461 else:
462 for i in xrange(times):
463 yield object
464
465
466.. function:: starmap(function, iterable)
467
Raymond Hettinger47317092008-01-17 03:02:14 +0000468 Make an iterator that computes the function using arguments obtained from
Georg Brandl8ec7f652007-08-15 14:28:01 +0000469 the iterable. Used instead of :func:`imap` when argument parameters are already
470 grouped in tuples from a single iterable (the data has been "pre-zipped"). The
471 difference between :func:`imap` and :func:`starmap` parallels the distinction
472 between ``function(a,b)`` and ``function(*c)``. Equivalent to::
473
474 def starmap(function, iterable):
Raymond Hettinger47317092008-01-17 03:02:14 +0000475 for args in iterable:
476 yield function(*args)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000477
Raymond Hettinger47317092008-01-17 03:02:14 +0000478 .. versionchanged:: 2.6
479 Previously, :func:`starmap` required the function arguments to be tuples.
480 Now, any iterable is allowed.
Georg Brandl8ec7f652007-08-15 14:28:01 +0000481
482.. function:: takewhile(predicate, iterable)
483
484 Make an iterator that returns elements from the iterable as long as the
485 predicate is true. Equivalent to::
486
487 def takewhile(predicate, iterable):
488 for x in iterable:
489 if predicate(x):
490 yield x
491 else:
492 break
493
494
495.. function:: tee(iterable[, n=2])
496
497 Return *n* independent iterators from a single iterable. The case where ``n==2``
498 is equivalent to::
499
500 def tee(iterable):
Raymond Hettinger5d332bb2007-12-29 22:09:34 +0000501 def gen(next, data={}):
Georg Brandl8ec7f652007-08-15 14:28:01 +0000502 for i in count():
Raymond Hettinger5d332bb2007-12-29 22:09:34 +0000503 if i in data:
504 yield data.pop(i)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000505 else:
Raymond Hettinger5d332bb2007-12-29 22:09:34 +0000506 data[i] = next()
507 yield data[i]
Georg Brandl8ec7f652007-08-15 14:28:01 +0000508 it = iter(iterable)
Raymond Hettinger5d332bb2007-12-29 22:09:34 +0000509 return gen(it.next), gen(it.next)
Georg Brandl8ec7f652007-08-15 14:28:01 +0000510
511 Note, once :func:`tee` has made a split, the original *iterable* should not be
512 used anywhere else; otherwise, the *iterable* could get advanced without the tee
513 objects being informed.
514
515 Note, this member of the toolkit may require significant auxiliary storage
516 (depending on how much temporary data needs to be stored). In general, if one
517 iterator is going to use most or all of the data before the other iterator, it
518 is faster to use :func:`list` instead of :func:`tee`.
519
520 .. versionadded:: 2.4
521
522
523.. _itertools-example:
524
525Examples
526--------
527
528The following examples show common uses for each tool and demonstrate ways they
529can be combined. ::
530
531 >>> amounts = [120.15, 764.05, 823.14]
532 >>> for checknum, amount in izip(count(1200), amounts):
533 ... print 'Check %d is for $%.2f' % (checknum, amount)
534 ...
535 Check 1200 is for $120.15
536 Check 1201 is for $764.05
537 Check 1202 is for $823.14
538
539 >>> import operator
540 >>> for cube in imap(operator.pow, xrange(1,5), repeat(3)):
541 ... print cube
542 ...
543 1
544 8
545 27
546 64
547
Georg Brandl8ec7f652007-08-15 14:28:01 +0000548 # Show a dictionary sorted and grouped by value
549 >>> from operator import itemgetter
550 >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
551 >>> di = sorted(d.iteritems(), key=itemgetter(1))
552 >>> for k, g in groupby(di, key=itemgetter(1)):
553 ... print k, map(itemgetter(0), g)
554 ...
555 1 ['a', 'c', 'e']
556 2 ['b', 'd', 'f']
557 3 ['g']
558
559 # Find runs of consecutive numbers using groupby. The key to the solution
560 # is differencing with a range so that consecutive numbers all appear in
561 # same group.
562 >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28]
563 >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x):
564 ... print map(operator.itemgetter(1), g)
565 ...
566 [1]
567 [4, 5, 6]
568 [10]
569 [15, 16, 17, 18]
570 [22]
571 [25, 26, 27, 28]
572
573
574
575.. _itertools-recipes:
576
577Recipes
578-------
579
580This section shows recipes for creating an extended toolset using the existing
581itertools as building blocks.
582
583The extended tools offer the same high performance as the underlying toolset.
584The superior memory performance is kept by processing elements one at a time
585rather than bringing the whole iterable into memory all at once. Code volume is
586kept small by linking the tools together in a functional style which helps
587eliminate temporary variables. High speed is retained by preferring
Georg Brandlcf3fb252007-10-21 10:52:38 +0000588"vectorized" building blocks over the use of for-loops and :term:`generator`\s
589which incur interpreter overhead. ::
Georg Brandl8ec7f652007-08-15 14:28:01 +0000590
591 def take(n, seq):
592 return list(islice(seq, n))
593
594 def enumerate(iterable):
595 return izip(count(), iterable)
596
597 def tabulate(function):
598 "Return function(0), function(1), ..."
599 return imap(function, count())
600
601 def iteritems(mapping):
602 return izip(mapping.iterkeys(), mapping.itervalues())
603
604 def nth(iterable, n):
605 "Returns the nth item or raise StopIteration"
606 return islice(iterable, n, None).next()
607
608 def all(seq, pred=None):
609 "Returns True if pred(x) is true for every element in the iterable"
610 for elem in ifilterfalse(pred, seq):
611 return False
612 return True
613
614 def any(seq, pred=None):
615 "Returns True if pred(x) is true for at least one element in the iterable"
616 for elem in ifilter(pred, seq):
617 return True
618 return False
619
620 def no(seq, pred=None):
621 "Returns True if pred(x) is false for every element in the iterable"
622 for elem in ifilter(pred, seq):
623 return False
624 return True
625
626 def quantify(seq, pred=None):
627 "Count how many times the predicate is true in the sequence"
628 return sum(imap(pred, seq))
629
630 def padnone(seq):
631 """Returns the sequence elements and then returns None indefinitely.
632
633 Useful for emulating the behavior of the built-in map() function.
634 """
635 return chain(seq, repeat(None))
636
637 def ncycles(seq, n):
638 "Returns the sequence elements n times"
Raymond Hettinger330958e62008-02-28 19:41:24 +0000639 return chain.from_iterable(repeat(seq, n))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000640
641 def dotproduct(vec1, vec2):
642 return sum(imap(operator.mul, vec1, vec2))
643
644 def flatten(listOfLists):
Raymond Hettinger330958e62008-02-28 19:41:24 +0000645 return list(chain.from_iterable(listOfLists))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000646
647 def repeatfunc(func, times=None, *args):
648 """Repeat calls to func with specified arguments.
649
650 Example: repeatfunc(random.random)
651 """
652 if times is None:
653 return starmap(func, repeat(args))
Raymond Hettinger330958e62008-02-28 19:41:24 +0000654 return starmap(func, repeat(args, times))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000655
656 def pairwise(iterable):
657 "s -> (s0,s1), (s1,s2), (s2, s3), ..."
658 a, b = tee(iterable)
659 try:
660 b.next()
661 except StopIteration:
662 pass
663 return izip(a, b)
664
665 def grouper(n, iterable, padvalue=None):
666 "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
667 return izip(*[chain(iterable, repeat(padvalue, n-1))]*n)
668
Raymond Hettingera44327a2008-01-30 22:17:31 +0000669 def roundrobin(*iterables):
670 "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
Raymond Hettinger330958e62008-02-28 19:41:24 +0000671 # Recipe credited to George Sakkis
Raymond Hettingera44327a2008-01-30 22:17:31 +0000672 pending = len(iterables)
673 nexts = cycle(iter(it).next for it in iterables)
674 while pending:
675 try:
676 for next in nexts:
677 yield next()
678 except StopIteration:
679 pending -= 1
680 nexts = cycle(islice(nexts, pending))
Georg Brandl8ec7f652007-08-15 14:28:01 +0000681
Raymond Hettinger7832d4d2008-02-23 10:04:15 +0000682 def powerset(iterable):
Raymond Hettinger330958e62008-02-28 19:41:24 +0000683 "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])"
684 # Recipe credited to Eric Raymond
685 pairs = [(2**i, x) for i, x in enumerate(iterable)]
686 for n in xrange(2**len(pairs)):
687 yield set(x for m, x in pairs if m&n)
Raymond Hettinger7832d4d2008-02-23 10:04:15 +0000688