Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 1 | |
| 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 Brandl | e7a0990 | 2007-10-21 12:10:28 +0000 | [diff] [blame] | 13 | This module implements a number of :term:`iterator` building blocks inspired by |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 14 | constructs from the Haskell and SML programming languages. Each has been recast |
| 15 | in a form suitable for Python. |
| 16 | |
| 17 | The module standardizes a core set of fast, memory efficient tools that are |
| 18 | useful by themselves or in combination. Standardization helps avoid the |
| 19 | readability and reliability problems which arise when many different individuals |
| 20 | create their own slightly varying implementations, each with their own quirks |
| 21 | and naming conventions. |
| 22 | |
| 23 | The tools are designed to combine readily with one another. This makes it easy |
| 24 | to construct more specialized tools succinctly and efficiently in pure Python. |
| 25 | |
| 26 | For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a |
| 27 | sequence ``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 |
| 29 | equivalent result. |
| 30 | |
| 31 | Likewise, the functional tools are designed to work well with the high-speed |
| 32 | functions provided by the :mod:`operator` module. |
| 33 | |
| 34 | The module author welcomes suggestions for other basic building blocks to be |
| 35 | added to future versions of the module. |
| 36 | |
| 37 | Whether cast in pure python form or compiled code, tools that use iterators are |
| 38 | more memory efficient (and faster) than their list based counterparts. Adopting |
| 39 | the principles of just-in-time manufacturing, they create data when and where |
| 40 | needed instead of consuming memory with the computer equivalent of "inventory". |
| 41 | |
| 42 | The performance advantage of iterators becomes more acute as the number of |
| 43 | elements increases -- at some point, lists grow large enough to severely impact |
| 44 | memory 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 | |
| 58 | Itertool functions |
| 59 | ------------------ |
| 60 | |
| 61 | The following module functions all construct and return iterators. Some provide |
| 62 | streams of infinite length, so they should only be accessed by functions or |
| 63 | loops 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 Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 79 | .. 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 Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 92 | .. function:: combinations(iterable, r) |
| 93 | |
| 94 | Return successive *r* length combinations of elements in the *iterable*. |
| 95 | |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 96 | Combinations are emitted in lexicographic sort order. So, if the |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 97 | input *iterable* is sorted, the combination tuples will be produced |
| 98 | in sorted order. |
| 99 | |
| 100 | Elements are treated as unique based on their position, not on their |
| 101 | value. So if the input elements are unique, there will be no repeat |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 102 | values in each combination. |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 103 | |
| 104 | Each result tuple is ordered to match the input order. So, every |
| 105 | combination is a subsequence of the input *iterable*. |
| 106 | |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 107 | Equivalent to:: |
| 108 | |
| 109 | def combinations(iterable, r): |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 110 | 'combinations(range(4), 3) --> (0,1,2) (0,1,3) (0,2,3) (1,2,3)' |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 111 | pool = tuple(iterable) |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 112 | n = len(pool) |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 113 | indices = range(r) |
| 114 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 115 | while 1: |
| 116 | for i in reversed(range(r)): |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 117 | if indices[i] != i + n - r: |
Raymond Hettinger | c105289 | 2008-02-27 01:44:34 +0000 | [diff] [blame] | 118 | break |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 119 | else: |
| 120 | return |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 121 | indices[i] += 1 |
Raymond Hettinger | c105289 | 2008-02-27 01:44:34 +0000 | [diff] [blame] | 122 | for j in range(i+1, r): |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 123 | indices[j] = indices[j-1] + 1 |
| 124 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 125 | |
| 126 | .. versionadded:: 2.6 |
| 127 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 128 | .. function:: count([n]) |
| 129 | |
| 130 | Make an iterator that returns consecutive integers starting with *n*. If not |
Raymond Hettinger | 50e90e2 | 2007-10-04 00:20:27 +0000 | [diff] [blame] | 131 | specified *n* defaults to zero. Often used as an argument to :func:`imap` to |
| 132 | generate consecutive data points. Also, used with :func:`izip` to add sequence |
| 133 | numbers. Equivalent to:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 134 | |
| 135 | def count(n=0): |
| 136 | while True: |
| 137 | yield n |
| 138 | n += 1 |
| 139 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 140 | |
| 141 | .. function:: cycle(iterable) |
| 142 | |
| 143 | Make an iterator returning elements from the iterable and saving a copy of each. |
| 144 | When the iterable is exhausted, return elements from the saved copy. Repeats |
| 145 | indefinitely. Equivalent to:: |
| 146 | |
| 147 | def cycle(iterable): |
| 148 | saved = [] |
| 149 | for element in iterable: |
| 150 | yield element |
| 151 | saved.append(element) |
| 152 | while saved: |
| 153 | for element in saved: |
| 154 | yield element |
| 155 | |
| 156 | Note, this member of the toolkit may require significant auxiliary storage |
| 157 | (depending on the length of the iterable). |
| 158 | |
| 159 | |
| 160 | .. function:: dropwhile(predicate, iterable) |
| 161 | |
| 162 | Make an iterator that drops elements from the iterable as long as the predicate |
| 163 | is true; afterwards, returns every element. Note, the iterator does not produce |
| 164 | *any* output until the predicate first becomes false, so it may have a lengthy |
| 165 | start-up time. Equivalent to:: |
| 166 | |
| 167 | def dropwhile(predicate, iterable): |
| 168 | iterable = iter(iterable) |
| 169 | for x in iterable: |
| 170 | if not predicate(x): |
| 171 | yield x |
| 172 | break |
| 173 | for x in iterable: |
| 174 | yield x |
| 175 | |
| 176 | |
| 177 | .. function:: groupby(iterable[, key]) |
| 178 | |
| 179 | Make an iterator that returns consecutive keys and groups from the *iterable*. |
| 180 | The *key* is a function computing a key value for each element. If not |
| 181 | specified or is ``None``, *key* defaults to an identity function and returns |
| 182 | the element unchanged. Generally, the iterable needs to already be sorted on |
| 183 | the same key function. |
| 184 | |
| 185 | The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It |
| 186 | generates a break or new group every time the value of the key function changes |
| 187 | (which is why it is usually necessary to have sorted the data using the same key |
| 188 | function). That behavior differs from SQL's GROUP BY which aggregates common |
| 189 | elements regardless of their input order. |
| 190 | |
| 191 | The returned group is itself an iterator that shares the underlying iterable |
| 192 | with :func:`groupby`. Because the source is shared, when the :func:`groupby` |
| 193 | object is advanced, the previous group is no longer visible. So, if that data |
| 194 | is needed later, it should be stored as a list:: |
| 195 | |
| 196 | groups = [] |
| 197 | uniquekeys = [] |
| 198 | data = sorted(data, key=keyfunc) |
| 199 | for k, g in groupby(data, keyfunc): |
| 200 | groups.append(list(g)) # Store group iterator as a list |
| 201 | uniquekeys.append(k) |
| 202 | |
| 203 | :func:`groupby` is equivalent to:: |
| 204 | |
| 205 | class groupby(object): |
| 206 | def __init__(self, iterable, key=None): |
| 207 | if key is None: |
| 208 | key = lambda x: x |
| 209 | self.keyfunc = key |
| 210 | self.it = iter(iterable) |
Raymond Hettinger | 81a885a | 2007-12-29 22:16:24 +0000 | [diff] [blame] | 211 | self.tgtkey = self.currkey = self.currvalue = object() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 212 | def __iter__(self): |
| 213 | return self |
| 214 | def next(self): |
| 215 | while self.currkey == self.tgtkey: |
| 216 | self.currvalue = self.it.next() # Exit on StopIteration |
| 217 | self.currkey = self.keyfunc(self.currvalue) |
| 218 | self.tgtkey = self.currkey |
| 219 | return (self.currkey, self._grouper(self.tgtkey)) |
| 220 | def _grouper(self, tgtkey): |
| 221 | while self.currkey == tgtkey: |
| 222 | yield self.currvalue |
| 223 | self.currvalue = self.it.next() # Exit on StopIteration |
| 224 | self.currkey = self.keyfunc(self.currvalue) |
| 225 | |
| 226 | .. versionadded:: 2.4 |
| 227 | |
| 228 | |
| 229 | .. function:: ifilter(predicate, iterable) |
| 230 | |
| 231 | Make an iterator that filters elements from iterable returning only those for |
| 232 | which the predicate is ``True``. If *predicate* is ``None``, return the items |
| 233 | that are true. Equivalent to:: |
| 234 | |
| 235 | def ifilter(predicate, iterable): |
| 236 | if predicate is None: |
| 237 | predicate = bool |
| 238 | for x in iterable: |
| 239 | if predicate(x): |
| 240 | yield x |
| 241 | |
| 242 | |
| 243 | .. function:: ifilterfalse(predicate, iterable) |
| 244 | |
| 245 | Make an iterator that filters elements from iterable returning only those for |
| 246 | which the predicate is ``False``. If *predicate* is ``None``, return the items |
| 247 | that are false. Equivalent to:: |
| 248 | |
| 249 | def ifilterfalse(predicate, iterable): |
| 250 | if predicate is None: |
| 251 | predicate = bool |
| 252 | for x in iterable: |
| 253 | if not predicate(x): |
| 254 | yield x |
| 255 | |
| 256 | |
| 257 | .. function:: imap(function, *iterables) |
| 258 | |
| 259 | Make an iterator that computes the function using arguments from each of the |
| 260 | iterables. If *function* is set to ``None``, then :func:`imap` returns the |
| 261 | arguments as a tuple. Like :func:`map` but stops when the shortest iterable is |
| 262 | exhausted instead of filling in ``None`` for shorter iterables. The reason for |
| 263 | the difference is that infinite iterator arguments are typically an error for |
| 264 | :func:`map` (because the output is fully evaluated) but represent a common and |
| 265 | useful way of supplying arguments to :func:`imap`. Equivalent to:: |
| 266 | |
| 267 | def imap(function, *iterables): |
| 268 | iterables = map(iter, iterables) |
| 269 | while True: |
Raymond Hettinger | 2dec48d | 2008-01-22 22:09:26 +0000 | [diff] [blame] | 270 | args = [it.next() for it in iterables] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 271 | if function is None: |
| 272 | yield tuple(args) |
| 273 | else: |
| 274 | yield function(*args) |
| 275 | |
| 276 | |
| 277 | .. function:: islice(iterable, [start,] stop [, step]) |
| 278 | |
| 279 | Make an iterator that returns selected elements from the iterable. If *start* is |
| 280 | non-zero, then elements from the iterable are skipped until start is reached. |
| 281 | Afterward, elements are returned consecutively unless *step* is set higher than |
| 282 | one which results in items being skipped. If *stop* is ``None``, then iteration |
| 283 | continues until the iterator is exhausted, if at all; otherwise, it stops at the |
| 284 | specified position. Unlike regular slicing, :func:`islice` does not support |
| 285 | negative values for *start*, *stop*, or *step*. Can be used to extract related |
| 286 | fields from data where the internal structure has been flattened (for example, a |
| 287 | multi-line report may list a name field on every third line). Equivalent to:: |
| 288 | |
| 289 | def islice(iterable, *args): |
| 290 | s = slice(*args) |
| 291 | it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1)) |
| 292 | nexti = it.next() |
| 293 | for i, element in enumerate(iterable): |
| 294 | if i == nexti: |
| 295 | yield element |
| 296 | nexti = it.next() |
| 297 | |
| 298 | If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, |
| 299 | then the step defaults to one. |
| 300 | |
| 301 | .. versionchanged:: 2.5 |
| 302 | accept ``None`` values for default *start* and *step*. |
| 303 | |
| 304 | |
| 305 | .. function:: izip(*iterables) |
| 306 | |
| 307 | Make an iterator that aggregates elements from each of the iterables. Like |
| 308 | :func:`zip` except that it returns an iterator instead of a list. Used for |
| 309 | lock-step iteration over several iterables at a time. Equivalent to:: |
| 310 | |
| 311 | def izip(*iterables): |
| 312 | iterables = map(iter, iterables) |
| 313 | while iterables: |
| 314 | result = [it.next() for it in iterables] |
| 315 | yield tuple(result) |
| 316 | |
| 317 | .. versionchanged:: 2.4 |
| 318 | When no iterables are specified, returns a zero length iterator instead of |
| 319 | raising a :exc:`TypeError` exception. |
| 320 | |
Raymond Hettinger | 48c6293 | 2008-01-22 19:51:41 +0000 | [diff] [blame] | 321 | 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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 324 | |
Raymond Hettinger | 48c6293 | 2008-01-22 19:51:41 +0000 | [diff] [blame] | 325 | :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 Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 328 | |
| 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 | |
| 352 | .. versionadded:: 2.6 |
| 353 | |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 354 | .. function:: permutations(iterable[, r]) |
| 355 | |
| 356 | Return successive *r* length permutations of elements in the *iterable*. |
| 357 | |
| 358 | If *r* is not specified or is ``None``, then *r* defaults to the length |
| 359 | of the *iterable* and all possible full-length permutations |
| 360 | are generated. |
| 361 | |
| 362 | Permutations are emitted in lexicographic sort order. So, if the |
| 363 | input *iterable* is sorted, the permutation tuples will be produced |
| 364 | in sorted order. |
| 365 | |
| 366 | Elements are treated as unique based on their position, not on their |
| 367 | value. So if the input elements are unique, there will be no repeat |
| 368 | values in each permutation. |
| 369 | |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame^] | 370 | Equivalent to:: |
| 371 | |
| 372 | def permutations(iterable, r=None): |
| 373 | 'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)' |
| 374 | pool = tuple(iterable) |
| 375 | n = len(pool) |
| 376 | r = n if r is None else r |
| 377 | indices = range(n) |
| 378 | cycles = range(n-r+1, n+1)[::-1] |
| 379 | yield tuple(pool[i] for i in indices[:r]) |
| 380 | while n: |
| 381 | for i in reversed(range(r)): |
| 382 | cycles[i] -= 1 |
| 383 | if cycles[i] == 0: |
| 384 | indices[:] = indices[:i] + indices[i+1:] + indices[i:i+1] |
| 385 | cycles[i] = n - i |
| 386 | else: |
| 387 | j = cycles[i] |
| 388 | indices[i], indices[-j] = indices[-j], indices[i] |
| 389 | yield tuple(pool[i] for i in indices[:r]) |
| 390 | break |
| 391 | else: |
| 392 | return |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 393 | |
| 394 | .. versionadded:: 2.6 |
| 395 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 396 | .. function:: product(*iterables[, repeat]) |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 397 | |
| 398 | Cartesian product of input iterables. |
| 399 | |
| 400 | Equivalent to nested for-loops in a generator expression. For example, |
| 401 | ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. |
| 402 | |
| 403 | The leftmost iterators are in the outermost for-loop, so the output tuples |
| 404 | cycle in a manner similar to an odometer (with the rightmost element |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 405 | changing on every iteration). This results in a lexicographic ordering |
| 406 | so that if the inputs iterables are sorted, the product tuples are emitted |
| 407 | in sorted order. |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 408 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 409 | To compute the product of an iterable with itself, specify the number of |
| 410 | repetitions with the optional *repeat* keyword argument. For example, |
| 411 | ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``. |
| 412 | |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 413 | Equivalent to the following except that the actual implementation does not |
| 414 | build-up intermediate results in memory:: |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 415 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 416 | def product(*args, **kwds): |
| 417 | pools = map(tuple, args) * kwds.get('repeat', 1) |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 418 | if pools: |
| 419 | result = [[]] |
| 420 | for pool in pools: |
| 421 | result = [x+[y] for x in result for y in pool] |
| 422 | for prod in result: |
| 423 | yield tuple(prod) |
| 424 | |
| 425 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 426 | |
| 427 | .. function:: repeat(object[, times]) |
| 428 | |
| 429 | Make an iterator that returns *object* over and over again. Runs indefinitely |
| 430 | unless the *times* argument is specified. Used as argument to :func:`imap` for |
| 431 | invariant parameters to the called function. Also used with :func:`izip` to |
| 432 | create an invariant part of a tuple record. Equivalent to:: |
| 433 | |
| 434 | def repeat(object, times=None): |
| 435 | if times is None: |
| 436 | while True: |
| 437 | yield object |
| 438 | else: |
| 439 | for i in xrange(times): |
| 440 | yield object |
| 441 | |
| 442 | |
| 443 | .. function:: starmap(function, iterable) |
| 444 | |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 445 | Make an iterator that computes the function using arguments obtained from |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 446 | the iterable. Used instead of :func:`imap` when argument parameters are already |
| 447 | grouped in tuples from a single iterable (the data has been "pre-zipped"). The |
| 448 | difference between :func:`imap` and :func:`starmap` parallels the distinction |
| 449 | between ``function(a,b)`` and ``function(*c)``. Equivalent to:: |
| 450 | |
| 451 | def starmap(function, iterable): |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 452 | for args in iterable: |
| 453 | yield function(*args) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 454 | |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 455 | .. versionchanged:: 2.6 |
| 456 | Previously, :func:`starmap` required the function arguments to be tuples. |
| 457 | Now, any iterable is allowed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 458 | |
| 459 | .. function:: takewhile(predicate, iterable) |
| 460 | |
| 461 | Make an iterator that returns elements from the iterable as long as the |
| 462 | predicate is true. Equivalent to:: |
| 463 | |
| 464 | def takewhile(predicate, iterable): |
| 465 | for x in iterable: |
| 466 | if predicate(x): |
| 467 | yield x |
| 468 | else: |
| 469 | break |
| 470 | |
| 471 | |
| 472 | .. function:: tee(iterable[, n=2]) |
| 473 | |
| 474 | Return *n* independent iterators from a single iterable. The case where ``n==2`` |
| 475 | is equivalent to:: |
| 476 | |
| 477 | def tee(iterable): |
Raymond Hettinger | 5d332bb | 2007-12-29 22:09:34 +0000 | [diff] [blame] | 478 | def gen(next, data={}): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 479 | for i in count(): |
Raymond Hettinger | 5d332bb | 2007-12-29 22:09:34 +0000 | [diff] [blame] | 480 | if i in data: |
| 481 | yield data.pop(i) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 482 | else: |
Raymond Hettinger | 5d332bb | 2007-12-29 22:09:34 +0000 | [diff] [blame] | 483 | data[i] = next() |
| 484 | yield data[i] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 485 | it = iter(iterable) |
Raymond Hettinger | 5d332bb | 2007-12-29 22:09:34 +0000 | [diff] [blame] | 486 | return gen(it.next), gen(it.next) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 487 | |
| 488 | Note, once :func:`tee` has made a split, the original *iterable* should not be |
| 489 | used anywhere else; otherwise, the *iterable* could get advanced without the tee |
| 490 | objects being informed. |
| 491 | |
| 492 | Note, this member of the toolkit may require significant auxiliary storage |
| 493 | (depending on how much temporary data needs to be stored). In general, if one |
| 494 | iterator is going to use most or all of the data before the other iterator, it |
| 495 | is faster to use :func:`list` instead of :func:`tee`. |
| 496 | |
| 497 | .. versionadded:: 2.4 |
| 498 | |
| 499 | |
| 500 | .. _itertools-example: |
| 501 | |
| 502 | Examples |
| 503 | -------- |
| 504 | |
| 505 | The following examples show common uses for each tool and demonstrate ways they |
| 506 | can be combined. :: |
| 507 | |
| 508 | >>> amounts = [120.15, 764.05, 823.14] |
| 509 | >>> for checknum, amount in izip(count(1200), amounts): |
| 510 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 511 | ... |
| 512 | Check 1200 is for $120.15 |
| 513 | Check 1201 is for $764.05 |
| 514 | Check 1202 is for $823.14 |
| 515 | |
| 516 | >>> import operator |
| 517 | >>> for cube in imap(operator.pow, xrange(1,5), repeat(3)): |
| 518 | ... print cube |
| 519 | ... |
| 520 | 1 |
| 521 | 8 |
| 522 | 27 |
| 523 | 64 |
| 524 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 525 | # Show a dictionary sorted and grouped by value |
| 526 | >>> from operator import itemgetter |
| 527 | >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
| 528 | >>> di = sorted(d.iteritems(), key=itemgetter(1)) |
| 529 | >>> for k, g in groupby(di, key=itemgetter(1)): |
| 530 | ... print k, map(itemgetter(0), g) |
| 531 | ... |
| 532 | 1 ['a', 'c', 'e'] |
| 533 | 2 ['b', 'd', 'f'] |
| 534 | 3 ['g'] |
| 535 | |
| 536 | # Find runs of consecutive numbers using groupby. The key to the solution |
| 537 | # is differencing with a range so that consecutive numbers all appear in |
| 538 | # same group. |
| 539 | >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
| 540 | >>> for k, g in groupby(enumerate(data), lambda (i,x):i-x): |
| 541 | ... print map(operator.itemgetter(1), g) |
| 542 | ... |
| 543 | [1] |
| 544 | [4, 5, 6] |
| 545 | [10] |
| 546 | [15, 16, 17, 18] |
| 547 | [22] |
| 548 | [25, 26, 27, 28] |
| 549 | |
| 550 | |
| 551 | |
| 552 | .. _itertools-recipes: |
| 553 | |
| 554 | Recipes |
| 555 | ------- |
| 556 | |
| 557 | This section shows recipes for creating an extended toolset using the existing |
| 558 | itertools as building blocks. |
| 559 | |
| 560 | The extended tools offer the same high performance as the underlying toolset. |
| 561 | The superior memory performance is kept by processing elements one at a time |
| 562 | rather than bringing the whole iterable into memory all at once. Code volume is |
| 563 | kept small by linking the tools together in a functional style which helps |
| 564 | eliminate temporary variables. High speed is retained by preferring |
Georg Brandl | cf3fb25 | 2007-10-21 10:52:38 +0000 | [diff] [blame] | 565 | "vectorized" building blocks over the use of for-loops and :term:`generator`\s |
| 566 | which incur interpreter overhead. :: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 567 | |
| 568 | def take(n, seq): |
| 569 | return list(islice(seq, n)) |
| 570 | |
| 571 | def enumerate(iterable): |
| 572 | return izip(count(), iterable) |
| 573 | |
| 574 | def tabulate(function): |
| 575 | "Return function(0), function(1), ..." |
| 576 | return imap(function, count()) |
| 577 | |
| 578 | def iteritems(mapping): |
| 579 | return izip(mapping.iterkeys(), mapping.itervalues()) |
| 580 | |
| 581 | def nth(iterable, n): |
| 582 | "Returns the nth item or raise StopIteration" |
| 583 | return islice(iterable, n, None).next() |
| 584 | |
| 585 | def all(seq, pred=None): |
| 586 | "Returns True if pred(x) is true for every element in the iterable" |
| 587 | for elem in ifilterfalse(pred, seq): |
| 588 | return False |
| 589 | return True |
| 590 | |
| 591 | def any(seq, pred=None): |
| 592 | "Returns True if pred(x) is true for at least one element in the iterable" |
| 593 | for elem in ifilter(pred, seq): |
| 594 | return True |
| 595 | return False |
| 596 | |
| 597 | def no(seq, pred=None): |
| 598 | "Returns True if pred(x) is false for every element in the iterable" |
| 599 | for elem in ifilter(pred, seq): |
| 600 | return False |
| 601 | return True |
| 602 | |
| 603 | def quantify(seq, pred=None): |
| 604 | "Count how many times the predicate is true in the sequence" |
| 605 | return sum(imap(pred, seq)) |
| 606 | |
| 607 | def padnone(seq): |
| 608 | """Returns the sequence elements and then returns None indefinitely. |
| 609 | |
| 610 | Useful for emulating the behavior of the built-in map() function. |
| 611 | """ |
| 612 | return chain(seq, repeat(None)) |
| 613 | |
| 614 | def ncycles(seq, n): |
| 615 | "Returns the sequence elements n times" |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 616 | return chain.from_iterable(repeat(seq, n)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 617 | |
| 618 | def dotproduct(vec1, vec2): |
| 619 | return sum(imap(operator.mul, vec1, vec2)) |
| 620 | |
| 621 | def flatten(listOfLists): |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 622 | return list(chain.from_iterable(listOfLists)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 623 | |
| 624 | def repeatfunc(func, times=None, *args): |
| 625 | """Repeat calls to func with specified arguments. |
| 626 | |
| 627 | Example: repeatfunc(random.random) |
| 628 | """ |
| 629 | if times is None: |
| 630 | return starmap(func, repeat(args)) |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 631 | return starmap(func, repeat(args, times)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 632 | |
| 633 | def pairwise(iterable): |
| 634 | "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 635 | a, b = tee(iterable) |
| 636 | try: |
| 637 | b.next() |
| 638 | except StopIteration: |
| 639 | pass |
| 640 | return izip(a, b) |
| 641 | |
| 642 | def grouper(n, iterable, padvalue=None): |
| 643 | "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')" |
| 644 | return izip(*[chain(iterable, repeat(padvalue, n-1))]*n) |
| 645 | |
Raymond Hettinger | a44327a | 2008-01-30 22:17:31 +0000 | [diff] [blame] | 646 | def roundrobin(*iterables): |
| 647 | "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'" |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 648 | # Recipe credited to George Sakkis |
Raymond Hettinger | a44327a | 2008-01-30 22:17:31 +0000 | [diff] [blame] | 649 | pending = len(iterables) |
| 650 | nexts = cycle(iter(it).next for it in iterables) |
| 651 | while pending: |
| 652 | try: |
| 653 | for next in nexts: |
| 654 | yield next() |
| 655 | except StopIteration: |
| 656 | pending -= 1 |
| 657 | nexts = cycle(islice(nexts, pending)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 658 | |
Raymond Hettinger | 7832d4d | 2008-02-23 10:04:15 +0000 | [diff] [blame] | 659 | def powerset(iterable): |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 660 | "powerset('ab') --> set([]), set(['a']), set(['b']), set(['a', 'b'])" |
| 661 | # Recipe credited to Eric Raymond |
| 662 | pairs = [(2**i, x) for i, x in enumerate(iterable)] |
| 663 | for n in xrange(2**len(pairs)): |
| 664 | yield set(x for m, x in pairs if m&n) |
Raymond Hettinger | 7832d4d | 2008-02-23 10:04:15 +0000 | [diff] [blame] | 665 | |