Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +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 | |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 11 | .. testsetup:: |
| 12 | |
| 13 | from itertools import * |
| 14 | |
| 15 | |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 16 | This module implements a number of :term:`iterator` building blocks inspired by |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 17 | constructs from the Haskell and SML programming languages. Each has been recast |
| 18 | in a form suitable for Python. |
| 19 | |
| 20 | The module standardizes a core set of fast, memory efficient tools that are |
| 21 | useful by themselves or in combination. Standardization helps avoid the |
| 22 | readability and reliability problems which arise when many different individuals |
| 23 | create their own slightly varying implementations, each with their own quirks |
| 24 | and naming conventions. |
| 25 | |
| 26 | The tools are designed to combine readily with one another. This makes it easy |
| 27 | to construct more specialized tools succinctly and efficiently in pure Python. |
| 28 | |
| 29 | For instance, SML provides a tabulation tool: ``tabulate(f)`` which produces a |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 30 | sequence ``f(0), f(1), ...``. But, this effect can be achieved in Python |
| 31 | by combining :func:`map` and :func:`count` to form ``map(f, count())``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 32 | |
| 33 | Likewise, the functional tools are designed to work well with the high-speed |
| 34 | functions provided by the :mod:`operator` module. |
| 35 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 36 | Whether cast in pure python form or compiled code, tools that use iterators are |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 37 | more memory efficient (and often faster) than their list based counterparts. Adopting |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 38 | the principles of just-in-time manufacturing, they create data when and where |
| 39 | needed instead of consuming memory with the computer equivalent of "inventory". |
| 40 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 41 | |
| 42 | .. seealso:: |
| 43 | |
| 44 | The Standard ML Basis Library, `The Standard ML Basis Library |
| 45 | <http://www.standardml.org/Basis/>`_. |
| 46 | |
| 47 | Haskell, A Purely Functional Language, `Definition of Haskell and the Standard |
| 48 | Libraries <http://www.haskell.org/definition/>`_. |
| 49 | |
| 50 | |
| 51 | .. _itertools-functions: |
| 52 | |
| 53 | Itertool functions |
| 54 | ------------------ |
| 55 | |
| 56 | The following module functions all construct and return iterators. Some provide |
| 57 | streams of infinite length, so they should only be accessed by functions or |
| 58 | loops that truncate the stream. |
| 59 | |
| 60 | |
| 61 | .. function:: chain(*iterables) |
| 62 | |
| 63 | Make an iterator that returns elements from the first iterable until it is |
| 64 | exhausted, then proceeds to the next iterable, until all of the iterables are |
| 65 | exhausted. Used for treating consecutive sequences as a single sequence. |
| 66 | Equivalent to:: |
| 67 | |
| 68 | def chain(*iterables): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 69 | # chain('ABC', 'DEF') --> A B C D E F |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 70 | for it in iterables: |
| 71 | for element in it: |
| 72 | yield element |
| 73 | |
| 74 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 75 | .. function:: itertools.chain.from_iterable(iterable) |
| 76 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 77 | Alternate constructor for :func:`chain`. Gets chained inputs from a |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 78 | single iterable argument that is evaluated lazily. Equivalent to:: |
| 79 | |
| 80 | @classmethod |
| 81 | def from_iterable(iterables): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 82 | # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 83 | for it in iterables: |
| 84 | for element in it: |
| 85 | yield element |
| 86 | |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 87 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 88 | .. function:: combinations(iterable, r) |
| 89 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 90 | Return *r* length subsequences of elements from the input *iterable*. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 91 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 92 | Combinations are emitted in lexicographic sort order. So, if the |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 93 | input *iterable* is sorted, the combination tuples will be produced |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 94 | in sorted order. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 95 | |
| 96 | Elements are treated as unique based on their position, not on their |
| 97 | value. So if the input elements are unique, there will be no repeat |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 98 | values in each combination. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 99 | |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 100 | Equivalent to:: |
| 101 | |
| 102 | def combinations(iterable, r): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 103 | # combinations('ABCD', 2) --> AB AC AD BC BD CD |
| 104 | # combinations(range(4), 3) --> 012 013 023 123 |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 105 | pool = tuple(iterable) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 106 | n = len(pool) |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame] | 107 | if r > n: |
| 108 | return |
| 109 | indices = list(range(r)) |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 110 | yield tuple(pool[i] for i in indices) |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 111 | while 1: |
| 112 | for i in reversed(range(r)): |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 113 | if indices[i] != i + n - r: |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 114 | break |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 115 | else: |
| 116 | return |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 117 | indices[i] += 1 |
Christian Heimes | 380f7f2 | 2008-02-28 11:19:05 +0000 | [diff] [blame] | 118 | for j in range(i+1, r): |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 119 | indices[j] = indices[j-1] + 1 |
| 120 | yield tuple(pool[i] for i in indices) |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 121 | |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 122 | The code for :func:`combinations` can be also expressed as a subsequence |
| 123 | of :func:`permutations` after filtering entries where the elements are not |
| 124 | in sorted order (according to their position in the input pool):: |
| 125 | |
| 126 | def combinations(iterable, r): |
| 127 | pool = tuple(iterable) |
| 128 | n = len(pool) |
| 129 | for indices in permutations(range(n), r): |
| 130 | if sorted(indices) == list(indices): |
| 131 | yield tuple(pool[i] for i in indices) |
| 132 | |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame] | 133 | The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n`` |
| 134 | or zero when ``r > n``. |
Christian Heimes | 836baa5 | 2008-02-26 08:18:30 +0000 | [diff] [blame] | 135 | |
Raymond Hettinger | 6b3b0fc | 2009-01-26 02:56:58 +0000 | [diff] [blame^] | 136 | .. function:: compress(data, selectors) |
| 137 | |
| 138 | Make an iterator that filters elements from *data* returning only those that |
| 139 | have a corresponding element in *selectors* that evaluates to ``True``. |
| 140 | Stops when either the *data* or *selectors* iterables have been exhausted. |
| 141 | Equivalent to:: |
| 142 | |
| 143 | def compress(data, selectors): |
| 144 | # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F |
| 145 | return (d for d, s in zip(data, selectors) if s) |
| 146 | |
| 147 | .. versionadded:: 2.7 |
| 148 | |
| 149 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 150 | .. function:: count([n]) |
| 151 | |
| 152 | Make an iterator that returns consecutive integers starting with *n*. If not |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 153 | specified *n* defaults to zero. Often used as an argument to :func:`map` to |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 154 | generate consecutive data points. Also, used with :func:`zip` to add sequence |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 155 | numbers. Equivalent to:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 156 | |
| 157 | def count(n=0): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 158 | # count(10) --> 10 11 12 13 14 ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 159 | while True: |
| 160 | yield n |
| 161 | n += 1 |
| 162 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 163 | |
| 164 | .. function:: cycle(iterable) |
| 165 | |
| 166 | Make an iterator returning elements from the iterable and saving a copy of each. |
| 167 | When the iterable is exhausted, return elements from the saved copy. Repeats |
| 168 | indefinitely. Equivalent to:: |
| 169 | |
| 170 | def cycle(iterable): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 171 | # cycle('ABCD') --> A B C D A B C D A B C D ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 172 | saved = [] |
| 173 | for element in iterable: |
| 174 | yield element |
| 175 | saved.append(element) |
| 176 | while saved: |
| 177 | for element in saved: |
| 178 | yield element |
| 179 | |
| 180 | Note, this member of the toolkit may require significant auxiliary storage |
| 181 | (depending on the length of the iterable). |
| 182 | |
| 183 | |
| 184 | .. function:: dropwhile(predicate, iterable) |
| 185 | |
| 186 | Make an iterator that drops elements from the iterable as long as the predicate |
| 187 | is true; afterwards, returns every element. Note, the iterator does not produce |
| 188 | *any* output until the predicate first becomes false, so it may have a lengthy |
| 189 | start-up time. Equivalent to:: |
| 190 | |
| 191 | def dropwhile(predicate, iterable): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 192 | # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 193 | iterable = iter(iterable) |
| 194 | for x in iterable: |
| 195 | if not predicate(x): |
| 196 | yield x |
| 197 | break |
| 198 | for x in iterable: |
| 199 | yield x |
| 200 | |
| 201 | |
| 202 | .. function:: groupby(iterable[, key]) |
| 203 | |
| 204 | Make an iterator that returns consecutive keys and groups from the *iterable*. |
| 205 | The *key* is a function computing a key value for each element. If not |
| 206 | specified or is ``None``, *key* defaults to an identity function and returns |
| 207 | the element unchanged. Generally, the iterable needs to already be sorted on |
| 208 | the same key function. |
| 209 | |
| 210 | The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It |
| 211 | generates a break or new group every time the value of the key function changes |
| 212 | (which is why it is usually necessary to have sorted the data using the same key |
| 213 | function). That behavior differs from SQL's GROUP BY which aggregates common |
| 214 | elements regardless of their input order. |
| 215 | |
| 216 | The returned group is itself an iterator that shares the underlying iterable |
| 217 | with :func:`groupby`. Because the source is shared, when the :func:`groupby` |
| 218 | object is advanced, the previous group is no longer visible. So, if that data |
| 219 | is needed later, it should be stored as a list:: |
| 220 | |
| 221 | groups = [] |
| 222 | uniquekeys = [] |
| 223 | data = sorted(data, key=keyfunc) |
| 224 | for k, g in groupby(data, keyfunc): |
| 225 | groups.append(list(g)) # Store group iterator as a list |
| 226 | uniquekeys.append(k) |
| 227 | |
| 228 | :func:`groupby` is equivalent to:: |
| 229 | |
| 230 | class groupby(object): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 231 | # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B |
| 232 | # [(list(g)) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 233 | def __init__(self, iterable, key=None): |
| 234 | if key is None: |
| 235 | key = lambda x: x |
| 236 | self.keyfunc = key |
| 237 | self.it = iter(iterable) |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 238 | self.tgtkey = self.currkey = self.currvalue = object() |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 239 | def __iter__(self): |
| 240 | return self |
| 241 | def __next__(self): |
| 242 | while self.currkey == self.tgtkey: |
| 243 | self.currvalue = next(self.it) # Exit on StopIteration |
| 244 | self.currkey = self.keyfunc(self.currvalue) |
| 245 | self.tgtkey = self.currkey |
| 246 | return (self.currkey, self._grouper(self.tgtkey)) |
| 247 | def _grouper(self, tgtkey): |
| 248 | while self.currkey == tgtkey: |
| 249 | yield self.currvalue |
| 250 | self.currvalue = next(self.it) # Exit on StopIteration |
| 251 | self.currkey = self.keyfunc(self.currvalue) |
| 252 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 253 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 254 | .. function:: filterfalse(predicate, iterable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 255 | |
| 256 | Make an iterator that filters elements from iterable returning only those for |
| 257 | which the predicate is ``False``. If *predicate* is ``None``, return the items |
| 258 | that are false. Equivalent to:: |
| 259 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 260 | def filterfalse(predicate, iterable): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 261 | # filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 262 | if predicate is None: |
| 263 | predicate = bool |
| 264 | for x in iterable: |
| 265 | if not predicate(x): |
| 266 | yield x |
| 267 | |
| 268 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 269 | .. function:: islice(iterable, [start,] stop [, step]) |
| 270 | |
| 271 | Make an iterator that returns selected elements from the iterable. If *start* is |
| 272 | non-zero, then elements from the iterable are skipped until start is reached. |
| 273 | Afterward, elements are returned consecutively unless *step* is set higher than |
| 274 | one which results in items being skipped. If *stop* is ``None``, then iteration |
| 275 | continues until the iterator is exhausted, if at all; otherwise, it stops at the |
| 276 | specified position. Unlike regular slicing, :func:`islice` does not support |
| 277 | negative values for *start*, *stop*, or *step*. Can be used to extract related |
| 278 | fields from data where the internal structure has been flattened (for example, a |
| 279 | multi-line report may list a name field on every third line). Equivalent to:: |
| 280 | |
| 281 | def islice(iterable, *args): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 282 | # islice('ABCDEFG', 2) --> A B |
| 283 | # islice('ABCDEFG', 2, 4) --> C D |
| 284 | # islice('ABCDEFG', 2, None) --> C D E F G |
| 285 | # islice('ABCDEFG', 0, None, 2) --> A C E G |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 286 | s = slice(*args) |
Georg Brandl | f694518 | 2008-02-01 11:56:49 +0000 | [diff] [blame] | 287 | it = range(s.start or 0, s.stop or sys.maxsize, s.step or 1) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 288 | nexti = next(it) |
| 289 | for i, element in enumerate(iterable): |
| 290 | if i == nexti: |
| 291 | yield element |
| 292 | nexti = next(it) |
| 293 | |
| 294 | If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, |
| 295 | then the step defaults to one. |
| 296 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 297 | |
Raymond Hettinger | b0002d2 | 2008-03-13 01:41:43 +0000 | [diff] [blame] | 298 | .. function:: zip_longest(*iterables[, fillvalue]) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 299 | |
| 300 | Make an iterator that aggregates elements from each of the iterables. If the |
| 301 | iterables are of uneven length, missing values are filled-in with *fillvalue*. |
| 302 | Iteration continues until the longest iterable is exhausted. Equivalent to:: |
| 303 | |
Raymond Hettinger | a137e1f | 2008-03-13 02:43:14 +0000 | [diff] [blame] | 304 | def zip_longest(*args, fillvalue=None): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 305 | # zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 306 | def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): |
| 307 | yield counter() # yields the fillvalue, or raises IndexError |
| 308 | fillers = repeat(fillvalue) |
| 309 | iters = [chain(it, sentinel(), fillers) for it in args] |
| 310 | try: |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 311 | for tup in zip(*iters): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 312 | yield tup |
| 313 | except IndexError: |
| 314 | pass |
| 315 | |
Benjamin Peterson | 37d2fe0 | 2008-10-24 22:28:58 +0000 | [diff] [blame] | 316 | If one of the iterables is potentially infinite, then the :func:`zip_longest` |
| 317 | function should be wrapped with something that limits the number of calls |
| 318 | (for example :func:`islice` or :func:`takewhile`). If not specified, |
| 319 | *fillvalue* defaults to ``None``. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 320 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 321 | |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 322 | .. function:: permutations(iterable[, r]) |
| 323 | |
| 324 | Return successive *r* length permutations of elements in the *iterable*. |
| 325 | |
| 326 | If *r* is not specified or is ``None``, then *r* defaults to the length |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 327 | of the *iterable* and all possible full-length permutations |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 328 | are generated. |
| 329 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 330 | Permutations are emitted in lexicographic sort order. So, if the |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 331 | input *iterable* is sorted, the permutation tuples will be produced |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 332 | in sorted order. |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 333 | |
| 334 | Elements are treated as unique based on their position, not on their |
| 335 | value. So if the input elements are unique, there will be no repeat |
| 336 | values in each permutation. |
| 337 | |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 338 | Equivalent to:: |
| 339 | |
| 340 | def permutations(iterable, r=None): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 341 | # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC |
| 342 | # permutations(range(3)) --> 012 021 102 120 201 210 |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 343 | pool = tuple(iterable) |
| 344 | n = len(pool) |
| 345 | r = n if r is None else r |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame] | 346 | if r > n: |
| 347 | return |
| 348 | indices = list(range(n)) |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 349 | cycles = range(n, n-r, -1) |
Christian Heimes | b558a2e | 2008-03-02 22:46:37 +0000 | [diff] [blame] | 350 | yield tuple(pool[i] for i in indices[:r]) |
| 351 | while n: |
| 352 | for i in reversed(range(r)): |
| 353 | cycles[i] -= 1 |
| 354 | if cycles[i] == 0: |
| 355 | indices[i:] = indices[i+1:] + indices[i:i+1] |
| 356 | cycles[i] = n - i |
| 357 | else: |
| 358 | j = cycles[i] |
| 359 | indices[i], indices[-j] = indices[-j], indices[i] |
| 360 | yield tuple(pool[i] for i in indices[:r]) |
| 361 | break |
| 362 | else: |
| 363 | return |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 364 | |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 365 | The code for :func:`permutations` can be also expressed as a subsequence of |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 366 | :func:`product`, filtered to exclude entries with repeated elements (those |
| 367 | from the same position in the input pool):: |
| 368 | |
| 369 | def permutations(iterable, r=None): |
| 370 | pool = tuple(iterable) |
| 371 | n = len(pool) |
| 372 | r = n if r is None else r |
| 373 | for indices in product(range(n), repeat=r): |
| 374 | if len(set(indices)) == r: |
| 375 | yield tuple(pool[i] for i in indices) |
| 376 | |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame] | 377 | The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n`` |
| 378 | or zero when ``r > n``. |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 379 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 380 | .. function:: product(*iterables[, repeat]) |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 381 | |
| 382 | Cartesian product of input iterables. |
| 383 | |
| 384 | Equivalent to nested for-loops in a generator expression. For example, |
| 385 | ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. |
| 386 | |
Christian Heimes | dae2a89 | 2008-04-19 00:55:37 +0000 | [diff] [blame] | 387 | The nested loops cycle like an odometer with the rightmost element advancing |
| 388 | on every iteration. This pattern creates a lexicographic ordering so that if |
| 389 | the input's iterables are sorted, the product tuples are emitted in sorted |
| 390 | order. |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 391 | |
Christian Heimes | 9e7f1d2 | 2008-02-28 12:27:11 +0000 | [diff] [blame] | 392 | To compute the product of an iterable with itself, specify the number of |
| 393 | repetitions with the optional *repeat* keyword argument. For example, |
| 394 | ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``. |
| 395 | |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 396 | This function is equivalent to the following code, except that the |
| 397 | actual implementation does not build up intermediate results in memory:: |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 398 | |
Raymond Hettinger | a137e1f | 2008-03-13 02:43:14 +0000 | [diff] [blame] | 399 | def product(*args, repeat=1): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 400 | # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy |
| 401 | # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 |
Raymond Hettinger | a137e1f | 2008-03-13 02:43:14 +0000 | [diff] [blame] | 402 | pools = map(tuple, args) * repeat |
Christian Heimes | 7864476 | 2008-03-04 23:39:23 +0000 | [diff] [blame] | 403 | result = [[]] |
| 404 | for pool in pools: |
| 405 | result = [x+[y] for x in result for y in pool] |
| 406 | for prod in result: |
| 407 | yield tuple(prod) |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 408 | |
| 409 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 410 | .. function:: repeat(object[, times]) |
| 411 | |
| 412 | Make an iterator that returns *object* over and over again. Runs indefinitely |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 413 | unless the *times* argument is specified. Used as argument to :func:`map` for |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 414 | invariant parameters to the called function. Also used with :func:`zip` to |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 415 | create an invariant part of a tuple record. Equivalent to:: |
| 416 | |
| 417 | def repeat(object, times=None): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 418 | # repeat(10, 3) --> 10 10 10 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 419 | if times is None: |
| 420 | while True: |
| 421 | yield object |
| 422 | else: |
| 423 | for i in range(times): |
| 424 | yield object |
| 425 | |
| 426 | |
| 427 | .. function:: starmap(function, iterable) |
| 428 | |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 429 | Make an iterator that computes the function using arguments obtained from |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 430 | the iterable. Used instead of :func:`map` when argument parameters are already |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 431 | grouped in tuples from a single iterable (the data has been "pre-zipped"). The |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 432 | difference between :func:`map` and :func:`starmap` parallels the distinction |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 433 | between ``function(a,b)`` and ``function(*c)``. Equivalent to:: |
| 434 | |
| 435 | def starmap(function, iterable): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 436 | # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
Christian Heimes | 679db4a | 2008-01-18 09:56:22 +0000 | [diff] [blame] | 437 | for args in iterable: |
| 438 | yield function(*args) |
| 439 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 440 | |
| 441 | .. function:: takewhile(predicate, iterable) |
| 442 | |
| 443 | Make an iterator that returns elements from the iterable as long as the |
| 444 | predicate is true. Equivalent to:: |
| 445 | |
| 446 | def takewhile(predicate, iterable): |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 447 | # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 448 | for x in iterable: |
| 449 | if predicate(x): |
| 450 | yield x |
| 451 | else: |
| 452 | break |
| 453 | |
| 454 | |
| 455 | .. function:: tee(iterable[, n=2]) |
| 456 | |
| 457 | Return *n* independent iterators from a single iterable. The case where ``n==2`` |
| 458 | is equivalent to:: |
| 459 | |
| 460 | def tee(iterable): |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 461 | def gen(next, data={}): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 462 | for i in count(): |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 463 | if i in data: |
| 464 | yield data.pop(i) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 465 | else: |
Christian Heimes | 5b5e81c | 2007-12-31 16:14:33 +0000 | [diff] [blame] | 466 | data[i] = next() |
| 467 | yield data[i] |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 468 | it = iter(iterable) |
| 469 | return (gen(it.__next__), gen(it.__next__)) |
| 470 | |
| 471 | Note, once :func:`tee` has made a split, the original *iterable* should not be |
| 472 | used anywhere else; otherwise, the *iterable* could get advanced without the tee |
| 473 | objects being informed. |
| 474 | |
| 475 | Note, this member of the toolkit may require significant auxiliary storage |
| 476 | (depending on how much temporary data needs to be stored). In general, if one |
| 477 | iterator is going to use most or all of the data before the other iterator, it |
| 478 | is faster to use :func:`list` instead of :func:`tee`. |
| 479 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 480 | |
| 481 | .. _itertools-example: |
| 482 | |
| 483 | Examples |
| 484 | -------- |
| 485 | |
| 486 | The following examples show common uses for each tool and demonstrate ways they |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 487 | can be combined. |
| 488 | |
| 489 | .. doctest:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 490 | |
Georg Brandl | b1441c7 | 2009-01-03 22:33:39 +0000 | [diff] [blame] | 491 | >>> # Show a dictionary sorted and grouped by value |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 492 | >>> from operator import itemgetter |
| 493 | >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) |
Fred Drake | 2e74878 | 2007-09-04 17:33:11 +0000 | [diff] [blame] | 494 | >>> di = sorted(d.items(), key=itemgetter(1)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 495 | >>> for k, g in groupby(di, key=itemgetter(1)): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 496 | ... print(k, map(itemgetter(0), g)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 497 | ... |
| 498 | 1 ['a', 'c', 'e'] |
| 499 | 2 ['b', 'd', 'f'] |
| 500 | 3 ['g'] |
| 501 | |
Georg Brandl | b1441c7 | 2009-01-03 22:33:39 +0000 | [diff] [blame] | 502 | >>> # Find runs of consecutive numbers using groupby. The key to the solution |
| 503 | >>> # is differencing with a range so that consecutive numbers all appear in |
| 504 | >>> # same group. |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 505 | >>> data = [ 1, 4,5,6, 10, 15,16,17,18, 22, 25,26,27,28] |
| 506 | >>> for k, g in groupby(enumerate(data), lambda t:t[0]-t[1]): |
Georg Brandl | 6911e3c | 2007-09-04 07:15:32 +0000 | [diff] [blame] | 507 | ... print(map(operator.itemgetter(1), g)) |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 508 | ... |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 509 | [1] |
| 510 | [4, 5, 6] |
| 511 | [10] |
| 512 | [15, 16, 17, 18] |
| 513 | [22] |
| 514 | [25, 26, 27, 28] |
| 515 | |
| 516 | |
| 517 | |
| 518 | .. _itertools-recipes: |
| 519 | |
| 520 | Recipes |
| 521 | ------- |
| 522 | |
| 523 | This section shows recipes for creating an extended toolset using the existing |
| 524 | itertools as building blocks. |
| 525 | |
| 526 | The extended tools offer the same high performance as the underlying toolset. |
| 527 | The superior memory performance is kept by processing elements one at a time |
| 528 | rather than bringing the whole iterable into memory all at once. Code volume is |
| 529 | kept small by linking the tools together in a functional style which helps |
| 530 | eliminate temporary variables. High speed is retained by preferring |
Georg Brandl | 9afde1c | 2007-11-01 20:32:30 +0000 | [diff] [blame] | 531 | "vectorized" building blocks over the use of for-loops and :term:`generator`\s |
Christian Heimes | fe337bf | 2008-03-23 21:54:12 +0000 | [diff] [blame] | 532 | which incur interpreter overhead. |
| 533 | |
| 534 | .. testcode:: |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 535 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 536 | def take(n, iterable): |
| 537 | "Return first n items of the iterable as a list" |
| 538 | return list(islice(iterable, n)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 539 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 540 | def enumerate(iterable, start=0): |
| 541 | return zip(count(start), iterable) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 542 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 543 | def tabulate(function, start=0): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 544 | "Return function(0), function(1), ..." |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 545 | return map(function, count(start)) |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 546 | |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 547 | def nth(iterable, n): |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 548 | "Returns the nth item or empty list" |
| 549 | return list(islice(iterable, n, n+1)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 550 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 551 | def quantify(iterable, pred=bool): |
| 552 | "Count how many times the predicate is true" |
| 553 | return sum(map(pred, iterable)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 554 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 555 | def padnone(iterable): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 556 | """Returns the sequence elements and then returns None indefinitely. |
| 557 | |
| 558 | Useful for emulating the behavior of the built-in map() function. |
| 559 | """ |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 560 | return chain(iterable, repeat(None)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 561 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 562 | def ncycles(iterable, n): |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 563 | "Returns the sequence elements n times" |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 564 | return chain.from_iterable(repeat(iterable, n)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 565 | |
| 566 | def dotproduct(vec1, vec2): |
Raymond Hettinger | a6c6037 | 2008-03-13 01:26:19 +0000 | [diff] [blame] | 567 | return sum(map(operator.mul, vec1, vec2)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 568 | |
| 569 | def flatten(listOfLists): |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 570 | return list(chain.from_iterable(listOfLists)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 571 | |
| 572 | def repeatfunc(func, times=None, *args): |
| 573 | """Repeat calls to func with specified arguments. |
| 574 | |
| 575 | Example: repeatfunc(random.random) |
| 576 | """ |
| 577 | if times is None: |
| 578 | return starmap(func, repeat(args)) |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 579 | return starmap(func, repeat(args, times)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 580 | |
| 581 | def pairwise(iterable): |
| 582 | "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 583 | a, b = tee(iterable) |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 584 | for elem in b: |
| 585 | break |
| 586 | return zip(a, b) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 587 | |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 588 | def grouper(n, iterable, fillvalue=None): |
Raymond Hettinger | f5a2e47 | 2008-07-30 07:37:37 +0000 | [diff] [blame] | 589 | "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 590 | args = [iter(iterable)] * n |
Benjamin Peterson | d18de0e | 2008-07-31 20:21:46 +0000 | [diff] [blame] | 591 | return zip_longest(fillvalue=fillvalue, *args) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 592 | |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 593 | def roundrobin(*iterables): |
Raymond Hettinger | f5a2e47 | 2008-07-30 07:37:37 +0000 | [diff] [blame] | 594 | "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
Christian Heimes | 70e7ea2 | 2008-02-28 20:02:27 +0000 | [diff] [blame] | 595 | # Recipe credited to George Sakkis |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 596 | pending = len(iterables) |
Raymond Hettinger | dd1150e | 2008-03-13 02:39:40 +0000 | [diff] [blame] | 597 | nexts = cycle(iter(it).__next__ for it in iterables) |
Christian Heimes | 7b3ce6a | 2008-01-31 14:31:45 +0000 | [diff] [blame] | 598 | while pending: |
| 599 | try: |
| 600 | for next in nexts: |
| 601 | yield next() |
| 602 | except StopIteration: |
| 603 | pending -= 1 |
| 604 | nexts = cycle(islice(nexts, pending)) |
Georg Brandl | 116aa62 | 2007-08-15 14:28:22 +0000 | [diff] [blame] | 605 | |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 606 | def powerset(iterable): |
Raymond Hettinger | ace6733 | 2009-01-26 02:23:50 +0000 | [diff] [blame] | 607 | "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" |
| 608 | s = list(iterable) |
| 609 | return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) |
Christian Heimes | 90c3d9b | 2008-02-23 13:18:03 +0000 | [diff] [blame] | 610 | |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 611 | def combinations_with_replacement(iterable, r): |
Raymond Hettinger | 5bad41e | 2009-01-08 21:01:54 +0000 | [diff] [blame] | 612 | "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" |
| 613 | # number items returned: (n+r-1)! / r! / (n-1)! |
Georg Brandl | 3dbca81 | 2008-07-23 16:10:53 +0000 | [diff] [blame] | 614 | pool = tuple(iterable) |
| 615 | n = len(pool) |
| 616 | indices = [0] * r |
| 617 | yield tuple(pool[i] for i in indices) |
| 618 | while True: |
| 619 | for i in reversed(range(r)): |
| 620 | if indices[i] != n - 1: |
| 621 | break |
| 622 | else: |
| 623 | return |
| 624 | indices[i:] = [indices[i] + 1] * (r - i) |
| 625 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | ad9d96b | 2009-01-02 21:39:07 +0000 | [diff] [blame] | 626 | |
| 627 | def unique_everseen(iterable, key=None): |
| 628 | "List unique elements, preserving order. Remember all elements ever seen." |
| 629 | # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
Georg Brandl | 48310cd | 2009-01-03 21:18:54 +0000 | [diff] [blame] | 630 | # unique_everseen('ABBCcAD', str.lower) --> A B C D |
Raymond Hettinger | ad9d96b | 2009-01-02 21:39:07 +0000 | [diff] [blame] | 631 | seen = set() |
| 632 | seen_add = seen.add |
| 633 | if key is None: |
| 634 | for element in iterable: |
| 635 | if element not in seen: |
| 636 | seen_add(element) |
| 637 | yield element |
| 638 | else: |
| 639 | for element in iterable: |
| 640 | k = key(element) |
| 641 | if k not in seen: |
| 642 | seen_add(k) |
| 643 | yield element |
| 644 | |
| 645 | def unique_justseen(iterable, key=None): |
| 646 | "List unique elements, preserving order. Remember only the element just seen." |
| 647 | # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
| 648 | # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
| 649 | return map(next, map(itemgetter(1), groupby(iterable, key))) |