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 | |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 11 | .. testsetup:: |
| 12 | |
| 13 | from itertools import * |
| 14 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 15 | .. versionadded:: 2.3 |
| 16 | |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 17 | This module implements a number of :term:`iterator` building blocks inspired |
| 18 | by constructs from APL, Haskell, and SML. Each has been recast in a form |
| 19 | suitable for Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 20 | |
| 21 | The module standardizes a core set of fast, memory efficient tools that are |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 22 | useful by themselves or in combination. Together, they form an "iterator |
| 23 | algebra" making it possible to construct specialized tools succinctly and |
| 24 | efficiently in pure Python. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 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 |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 28 | :func:`count` which can be combined to form ``imap(f, count())`` to produce an |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 29 | equivalent result. |
| 30 | |
Raymond Hettinger | efa7c13 | 2009-03-12 00:31:58 +0000 | [diff] [blame] | 31 | These tools and their built-in counterparts also work well with the high-speed |
| 32 | functions in the :mod:`operator` module. For example, the multiplication |
| 33 | operator can be mapped across two vectors to form an efficient dot-product: |
| 34 | ``sum(imap(operator.mul, vector1, vector2))``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 35 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 36 | |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 37 | **Infinite Iterators:** |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 38 | |
Raymond Hettinger | f0f475d | 2009-04-10 13:16:50 +0000 | [diff] [blame] | 39 | ================== ================= ================================================= ========================================= |
| 40 | Iterator Arguments Results Example |
| 41 | ================== ================= ================================================= ========================================= |
| 42 | :func:`count` start, [step] start, start+step, start+2*step, ... ``count(10) --> 10 11 12 13 14 ...`` |
| 43 | :func:`cycle` p p0, p1, ... plast, p0, p1, ... ``cycle('ABCD') --> A B C D A B C D ...`` |
| 44 | :func:`repeat` elem [,n] elem, elem, elem, ... endlessly or up to n times ``repeat(10, 3) --> 10 10 10`` |
| 45 | ================== ================= ================================================= ========================================= |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 46 | |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 47 | **Iterators terminating on the shortest input sequence:** |
| 48 | |
Raymond Hettinger | f0f475d | 2009-04-10 13:16:50 +0000 | [diff] [blame] | 49 | ==================== ============================ ================================================= ============================================================= |
| 50 | Iterator Arguments Results Example |
| 51 | ==================== ============================ ================================================= ============================================================= |
| 52 | :func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') --> A B C D E F`` |
| 53 | :func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F`` |
| 54 | :func:`dropwhile` pred, seq seq[n], seq[n+1], starting when pred fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1`` |
| 55 | :func:`groupby` iterable[, keyfunc] sub-iterators grouped by value of keyfunc(v) |
| 56 | :func:`ifilter` pred, seq elements of seq where pred(elem) is True ``ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9`` |
| 57 | :func:`ifilterfalse` pred, seq elements of seq where pred(elem) is False ``ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8`` |
| 58 | :func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) --> C D E F G`` |
| 59 | :func:`imap` func, p, q, ... func(p0, q0), func(p1, q1), ... ``imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000`` |
| 60 | :func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000`` |
| 61 | :func:`tee` it, n it1, it2 , ... itn splits one iterator into n |
| 62 | :func:`takewhile` pred, seq seq[0], seq[1], until pred fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4`` |
| 63 | :func:`izip` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``izip('ABCD', 'xy') --> Ax By`` |
| 64 | :func:`izip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-`` |
| 65 | ==================== ============================ ================================================= ============================================================= |
Raymond Hettinger | 0aee942 | 2009-02-17 11:00:27 +0000 | [diff] [blame] | 66 | |
| 67 | **Combinatoric generators:** |
| 68 | |
Raymond Hettinger | f0f475d | 2009-04-10 13:16:50 +0000 | [diff] [blame] | 69 | ============================================== ==================== ============================================================= |
| 70 | Iterator Arguments Results |
| 71 | ============================================== ==================== ============================================================= |
| 72 | :func:`product` p, q, ... [repeat=1] cartesian product, equivalent to a nested for-loop |
| 73 | :func:`permutations` p[, r] r-length tuples, all possible orderings, no repeated elements |
Raymond Hettinger | 9eac119 | 2009-11-19 01:22:04 +0000 | [diff] [blame] | 74 | :func:`combinations` p, r r-length tuples, in sorted order, no repeated elements |
| 75 | :func:`combinations_with_replacement` p, r r-length tuples, in sorted order, with repeated elements |
Raymond Hettinger | f0f475d | 2009-04-10 13:16:50 +0000 | [diff] [blame] | 76 | | |
| 77 | ``product('ABCD', repeat=2)`` ``AA AB AC AD BA BB BC BD CA CB CC CD DA DB DC DD`` |
| 78 | ``permutations('ABCD', 2)`` ``AB AC AD BA BC BD CA CB CD DA DB DC`` |
| 79 | ``combinations('ABCD', 2)`` ``AB AC AD BC BD CD`` |
| 80 | ``combinations_with_replacement('ABCD', 2)`` ``AA AB AC AD BB BC BD CC CD DD`` |
| 81 | ============================================== ==================== ============================================================= |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 82 | |
| 83 | |
| 84 | .. _itertools-functions: |
| 85 | |
| 86 | Itertool functions |
| 87 | ------------------ |
| 88 | |
| 89 | The following module functions all construct and return iterators. Some provide |
| 90 | streams of infinite length, so they should only be accessed by functions or |
| 91 | loops that truncate the stream. |
| 92 | |
| 93 | |
| 94 | .. function:: chain(*iterables) |
| 95 | |
| 96 | Make an iterator that returns elements from the first iterable until it is |
| 97 | exhausted, then proceeds to the next iterable, until all of the iterables are |
| 98 | exhausted. Used for treating consecutive sequences as a single sequence. |
| 99 | Equivalent to:: |
| 100 | |
| 101 | def chain(*iterables): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 102 | # chain('ABC', 'DEF') --> A B C D E F |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 103 | for it in iterables: |
| 104 | for element in it: |
| 105 | yield element |
| 106 | |
| 107 | |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 108 | .. function:: itertools.chain.from_iterable(iterable) |
| 109 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 110 | Alternate constructor for :func:`chain`. Gets chained inputs from a |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 111 | single iterable argument that is evaluated lazily. Equivalent to:: |
| 112 | |
| 113 | @classmethod |
| 114 | def from_iterable(iterables): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 115 | # chain.from_iterable(['ABC', 'DEF']) --> A B C D E F |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 116 | for it in iterables: |
| 117 | for element in it: |
| 118 | yield element |
| 119 | |
| 120 | .. versionadded:: 2.6 |
| 121 | |
Raymond Hettinger | d553d85 | 2008-03-04 04:17:08 +0000 | [diff] [blame] | 122 | |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 123 | .. function:: combinations(iterable, r) |
| 124 | |
Raymond Hettinger | 5eaffc4 | 2008-04-17 10:48:31 +0000 | [diff] [blame] | 125 | Return *r* length subsequences of elements from the input *iterable*. |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 126 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 127 | Combinations are emitted in lexicographic sort order. So, if the |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 128 | input *iterable* is sorted, the combination tuples will be produced |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 129 | in sorted order. |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 130 | |
| 131 | Elements are treated as unique based on their position, not on their |
| 132 | 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] | 133 | values in each combination. |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 134 | |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 135 | Equivalent to:: |
| 136 | |
| 137 | def combinations(iterable, r): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 138 | # combinations('ABCD', 2) --> AB AC AD BC BD CD |
| 139 | # combinations(range(4), 3) --> 012 013 023 123 |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 140 | pool = tuple(iterable) |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 141 | n = len(pool) |
Raymond Hettinger | 5b913e3 | 2009-01-08 06:39:04 +0000 | [diff] [blame] | 142 | if r > n: |
| 143 | return |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 144 | indices = range(r) |
| 145 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 146 | while True: |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 147 | for i in reversed(range(r)): |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 148 | if indices[i] != i + n - r: |
Raymond Hettinger | c105289 | 2008-02-27 01:44:34 +0000 | [diff] [blame] | 149 | break |
Raymond Hettinger | 93e804d | 2008-02-26 23:40:50 +0000 | [diff] [blame] | 150 | else: |
| 151 | return |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 152 | indices[i] += 1 |
Raymond Hettinger | c105289 | 2008-02-27 01:44:34 +0000 | [diff] [blame] | 153 | for j in range(i+1, r): |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 154 | indices[j] = indices[j-1] + 1 |
| 155 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 156 | |
Raymond Hettinger | d553d85 | 2008-03-04 04:17:08 +0000 | [diff] [blame] | 157 | The code for :func:`combinations` can be also expressed as a subsequence |
| 158 | of :func:`permutations` after filtering entries where the elements are not |
| 159 | in sorted order (according to their position in the input pool):: |
| 160 | |
| 161 | def combinations(iterable, r): |
| 162 | pool = tuple(iterable) |
| 163 | n = len(pool) |
| 164 | for indices in permutations(range(n), r): |
| 165 | if sorted(indices) == list(indices): |
| 166 | yield tuple(pool[i] for i in indices) |
| 167 | |
Raymond Hettinger | 5b913e3 | 2009-01-08 06:39:04 +0000 | [diff] [blame] | 168 | The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n`` |
| 169 | or zero when ``r > n``. |
| 170 | |
Raymond Hettinger | 3fa41d5 | 2008-02-26 02:46:54 +0000 | [diff] [blame] | 171 | .. versionadded:: 2.6 |
| 172 | |
Raymond Hettinger | d081abc | 2009-01-27 02:58:49 +0000 | [diff] [blame] | 173 | .. function:: combinations_with_replacement(iterable, r) |
| 174 | |
| 175 | Return *r* length subsequences of elements from the input *iterable* |
| 176 | allowing individual elements to be repeated more than once. |
| 177 | |
| 178 | Combinations are emitted in lexicographic sort order. So, if the |
| 179 | input *iterable* is sorted, the combination tuples will be produced |
| 180 | in sorted order. |
| 181 | |
| 182 | Elements are treated as unique based on their position, not on their |
| 183 | value. So if the input elements are unique, the generated combinations |
| 184 | will also be unique. |
| 185 | |
| 186 | Equivalent to:: |
| 187 | |
| 188 | def combinations_with_replacement(iterable, r): |
| 189 | # combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC |
| 190 | pool = tuple(iterable) |
| 191 | n = len(pool) |
| 192 | if not n and r: |
| 193 | return |
| 194 | indices = [0] * r |
| 195 | yield tuple(pool[i] for i in indices) |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 196 | while True: |
Raymond Hettinger | d081abc | 2009-01-27 02:58:49 +0000 | [diff] [blame] | 197 | for i in reversed(range(r)): |
| 198 | if indices[i] != n - 1: |
| 199 | break |
| 200 | else: |
| 201 | return |
| 202 | indices[i:] = [indices[i] + 1] * (r - i) |
| 203 | yield tuple(pool[i] for i in indices) |
| 204 | |
| 205 | The code for :func:`combinations_with_replacement` can be also expressed as |
| 206 | a subsequence of :func:`product` after filtering entries where the elements |
| 207 | are not in sorted order (according to their position in the input pool):: |
| 208 | |
| 209 | def combinations_with_replacement(iterable, r): |
| 210 | pool = tuple(iterable) |
| 211 | n = len(pool) |
| 212 | for indices in product(range(n), repeat=r): |
| 213 | if sorted(indices) == list(indices): |
| 214 | yield tuple(pool[i] for i in indices) |
| 215 | |
| 216 | The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``. |
| 217 | |
| 218 | .. versionadded:: 2.7 |
| 219 | |
Raymond Hettinger | 2bcb8e9 | 2009-01-25 21:04:14 +0000 | [diff] [blame] | 220 | .. function:: compress(data, selectors) |
| 221 | |
| 222 | Make an iterator that filters elements from *data* returning only those that |
| 223 | have a corresponding element in *selectors* that evaluates to ``True``. |
Andrew M. Kuchling | efa9771 | 2009-03-30 23:08:24 +0000 | [diff] [blame] | 224 | Stops when either the *data* or *selectors* iterables has been exhausted. |
Raymond Hettinger | 2bcb8e9 | 2009-01-25 21:04:14 +0000 | [diff] [blame] | 225 | Equivalent to:: |
| 226 | |
| 227 | def compress(data, selectors): |
| 228 | # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F |
| 229 | return (d for d, s in izip(data, selectors) if s) |
| 230 | |
| 231 | .. versionadded:: 2.7 |
| 232 | |
| 233 | |
Raymond Hettinger | a403803 | 2009-02-14 00:25:51 +0000 | [diff] [blame] | 234 | .. function:: count(start=0, step=1) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 235 | |
Raymond Hettinger | 31c769c | 2009-02-12 05:39:46 +0000 | [diff] [blame] | 236 | Make an iterator that returns evenly spaced values starting with *n*. Often |
| 237 | used as an argument to :func:`imap` to generate consecutive data points. |
| 238 | Also, used with :func:`izip` to add sequence numbers. Equivalent to:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 239 | |
Raymond Hettinger | a403803 | 2009-02-14 00:25:51 +0000 | [diff] [blame] | 240 | def count(start=0, step=1): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 241 | # count(10) --> 10 11 12 13 14 ... |
Raymond Hettinger | 31c769c | 2009-02-12 05:39:46 +0000 | [diff] [blame] | 242 | # count(2.5, 0.5) -> 3.5 3.0 4.5 ... |
Raymond Hettinger | a403803 | 2009-02-14 00:25:51 +0000 | [diff] [blame] | 243 | n = start |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 244 | while True: |
| 245 | yield n |
Raymond Hettinger | 31c769c | 2009-02-12 05:39:46 +0000 | [diff] [blame] | 246 | n += step |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 247 | |
Raymond Hettinger | 3a02624 | 2009-06-17 01:43:47 +0000 | [diff] [blame] | 248 | When counting with floating point numbers, better accuracy can sometimes be |
| 249 | achieved by substituting multiplicative code such as: ``(start + step * i |
| 250 | for i in count())``. |
| 251 | |
Raymond Hettinger | 31c769c | 2009-02-12 05:39:46 +0000 | [diff] [blame] | 252 | .. versionchanged:: 2.7 |
| 253 | added *step* argument and allowed non-integer arguments. |
| 254 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 255 | .. function:: cycle(iterable) |
| 256 | |
| 257 | Make an iterator returning elements from the iterable and saving a copy of each. |
| 258 | When the iterable is exhausted, return elements from the saved copy. Repeats |
| 259 | indefinitely. Equivalent to:: |
| 260 | |
| 261 | def cycle(iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 262 | # cycle('ABCD') --> A B C D A B C D A B C D ... |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 263 | saved = [] |
| 264 | for element in iterable: |
| 265 | yield element |
| 266 | saved.append(element) |
| 267 | while saved: |
| 268 | for element in saved: |
| 269 | yield element |
| 270 | |
| 271 | Note, this member of the toolkit may require significant auxiliary storage |
| 272 | (depending on the length of the iterable). |
| 273 | |
| 274 | |
| 275 | .. function:: dropwhile(predicate, iterable) |
| 276 | |
| 277 | Make an iterator that drops elements from the iterable as long as the predicate |
| 278 | is true; afterwards, returns every element. Note, the iterator does not produce |
| 279 | *any* output until the predicate first becomes false, so it may have a lengthy |
| 280 | start-up time. Equivalent to:: |
| 281 | |
| 282 | def dropwhile(predicate, iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 283 | # dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 284 | iterable = iter(iterable) |
| 285 | for x in iterable: |
| 286 | if not predicate(x): |
| 287 | yield x |
| 288 | break |
| 289 | for x in iterable: |
| 290 | yield x |
| 291 | |
| 292 | |
| 293 | .. function:: groupby(iterable[, key]) |
| 294 | |
| 295 | Make an iterator that returns consecutive keys and groups from the *iterable*. |
| 296 | The *key* is a function computing a key value for each element. If not |
| 297 | specified or is ``None``, *key* defaults to an identity function and returns |
| 298 | the element unchanged. Generally, the iterable needs to already be sorted on |
| 299 | the same key function. |
| 300 | |
| 301 | The operation of :func:`groupby` is similar to the ``uniq`` filter in Unix. It |
| 302 | generates a break or new group every time the value of the key function changes |
| 303 | (which is why it is usually necessary to have sorted the data using the same key |
| 304 | function). That behavior differs from SQL's GROUP BY which aggregates common |
| 305 | elements regardless of their input order. |
| 306 | |
| 307 | The returned group is itself an iterator that shares the underlying iterable |
| 308 | with :func:`groupby`. Because the source is shared, when the :func:`groupby` |
| 309 | object is advanced, the previous group is no longer visible. So, if that data |
| 310 | is needed later, it should be stored as a list:: |
| 311 | |
| 312 | groups = [] |
| 313 | uniquekeys = [] |
| 314 | data = sorted(data, key=keyfunc) |
| 315 | for k, g in groupby(data, keyfunc): |
| 316 | groups.append(list(g)) # Store group iterator as a list |
| 317 | uniquekeys.append(k) |
| 318 | |
| 319 | :func:`groupby` is equivalent to:: |
| 320 | |
| 321 | class groupby(object): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 322 | # [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B |
Raymond Hettinger | d507afd | 2009-02-04 10:52:32 +0000 | [diff] [blame] | 323 | # [list(g) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 324 | def __init__(self, iterable, key=None): |
| 325 | if key is None: |
| 326 | key = lambda x: x |
| 327 | self.keyfunc = key |
| 328 | self.it = iter(iterable) |
Raymond Hettinger | 81a885a | 2007-12-29 22:16:24 +0000 | [diff] [blame] | 329 | self.tgtkey = self.currkey = self.currvalue = object() |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 330 | def __iter__(self): |
| 331 | return self |
| 332 | def next(self): |
| 333 | while self.currkey == self.tgtkey: |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 334 | self.currvalue = next(self.it) # Exit on StopIteration |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 335 | self.currkey = self.keyfunc(self.currvalue) |
| 336 | self.tgtkey = self.currkey |
| 337 | return (self.currkey, self._grouper(self.tgtkey)) |
| 338 | def _grouper(self, tgtkey): |
| 339 | while self.currkey == tgtkey: |
| 340 | yield self.currvalue |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 341 | self.currvalue = next(self.it) # Exit on StopIteration |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 342 | self.currkey = self.keyfunc(self.currvalue) |
| 343 | |
| 344 | .. versionadded:: 2.4 |
| 345 | |
| 346 | |
| 347 | .. function:: ifilter(predicate, iterable) |
| 348 | |
| 349 | Make an iterator that filters elements from iterable returning only those for |
| 350 | which the predicate is ``True``. If *predicate* is ``None``, return the items |
| 351 | that are true. Equivalent to:: |
| 352 | |
| 353 | def ifilter(predicate, iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 354 | # ifilter(lambda x: x%2, range(10)) --> 1 3 5 7 9 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 355 | if predicate is None: |
| 356 | predicate = bool |
| 357 | for x in iterable: |
| 358 | if predicate(x): |
| 359 | yield x |
| 360 | |
| 361 | |
| 362 | .. function:: ifilterfalse(predicate, iterable) |
| 363 | |
| 364 | Make an iterator that filters elements from iterable returning only those for |
| 365 | which the predicate is ``False``. If *predicate* is ``None``, return the items |
| 366 | that are false. Equivalent to:: |
| 367 | |
| 368 | def ifilterfalse(predicate, iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 369 | # ifilterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 370 | if predicate is None: |
| 371 | predicate = bool |
| 372 | for x in iterable: |
| 373 | if not predicate(x): |
| 374 | yield x |
| 375 | |
| 376 | |
| 377 | .. function:: imap(function, *iterables) |
| 378 | |
| 379 | Make an iterator that computes the function using arguments from each of the |
| 380 | iterables. If *function* is set to ``None``, then :func:`imap` returns the |
| 381 | arguments as a tuple. Like :func:`map` but stops when the shortest iterable is |
| 382 | exhausted instead of filling in ``None`` for shorter iterables. The reason for |
| 383 | the difference is that infinite iterator arguments are typically an error for |
| 384 | :func:`map` (because the output is fully evaluated) but represent a common and |
| 385 | useful way of supplying arguments to :func:`imap`. Equivalent to:: |
| 386 | |
| 387 | def imap(function, *iterables): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 388 | # imap(pow, (2,3,10), (5,2,3)) --> 32 9 1000 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 389 | iterables = map(iter, iterables) |
| 390 | while True: |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 391 | args = [next(it) for it in iterables] |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 392 | if function is None: |
| 393 | yield tuple(args) |
| 394 | else: |
| 395 | yield function(*args) |
| 396 | |
| 397 | |
| 398 | .. function:: islice(iterable, [start,] stop [, step]) |
| 399 | |
| 400 | Make an iterator that returns selected elements from the iterable. If *start* is |
| 401 | non-zero, then elements from the iterable are skipped until start is reached. |
| 402 | Afterward, elements are returned consecutively unless *step* is set higher than |
| 403 | one which results in items being skipped. If *stop* is ``None``, then iteration |
| 404 | continues until the iterator is exhausted, if at all; otherwise, it stops at the |
| 405 | specified position. Unlike regular slicing, :func:`islice` does not support |
| 406 | negative values for *start*, *stop*, or *step*. Can be used to extract related |
| 407 | fields from data where the internal structure has been flattened (for example, a |
| 408 | multi-line report may list a name field on every third line). Equivalent to:: |
| 409 | |
| 410 | def islice(iterable, *args): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 411 | # islice('ABCDEFG', 2) --> A B |
| 412 | # islice('ABCDEFG', 2, 4) --> C D |
| 413 | # islice('ABCDEFG', 2, None) --> C D E F G |
| 414 | # islice('ABCDEFG', 0, None, 2) --> A C E G |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 415 | s = slice(*args) |
| 416 | it = iter(xrange(s.start or 0, s.stop or sys.maxint, s.step or 1)) |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 417 | nexti = next(it) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 418 | for i, element in enumerate(iterable): |
| 419 | if i == nexti: |
| 420 | yield element |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 421 | nexti = next(it) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 422 | |
| 423 | If *start* is ``None``, then iteration starts at zero. If *step* is ``None``, |
| 424 | then the step defaults to one. |
| 425 | |
| 426 | .. versionchanged:: 2.5 |
| 427 | accept ``None`` values for default *start* and *step*. |
| 428 | |
| 429 | |
| 430 | .. function:: izip(*iterables) |
| 431 | |
| 432 | Make an iterator that aggregates elements from each of the iterables. Like |
| 433 | :func:`zip` except that it returns an iterator instead of a list. Used for |
| 434 | lock-step iteration over several iterables at a time. Equivalent to:: |
| 435 | |
| 436 | def izip(*iterables): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 437 | # izip('ABCD', 'xy') --> Ax By |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 438 | iterables = map(iter, iterables) |
| 439 | while iterables: |
Raymond Hettinger | cccfc82 | 2009-04-20 18:23:57 +0000 | [diff] [blame] | 440 | yield tuple(map(next, iterables)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 441 | |
| 442 | .. versionchanged:: 2.4 |
| 443 | When no iterables are specified, returns a zero length iterator instead of |
| 444 | raising a :exc:`TypeError` exception. |
| 445 | |
Raymond Hettinger | 48c6293 | 2008-01-22 19:51:41 +0000 | [diff] [blame] | 446 | The left-to-right evaluation order of the iterables is guaranteed. This |
| 447 | makes possible an idiom for clustering a data series into n-length groups |
| 448 | using ``izip(*[iter(s)]*n)``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 449 | |
Raymond Hettinger | 48c6293 | 2008-01-22 19:51:41 +0000 | [diff] [blame] | 450 | :func:`izip` should only be used with unequal length inputs when you don't |
| 451 | care about trailing, unmatched values from the longer iterables. If those |
| 452 | values are important, use :func:`izip_longest` instead. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 453 | |
| 454 | |
| 455 | .. function:: izip_longest(*iterables[, fillvalue]) |
| 456 | |
| 457 | Make an iterator that aggregates elements from each of the iterables. If the |
| 458 | iterables are of uneven length, missing values are filled-in with *fillvalue*. |
| 459 | Iteration continues until the longest iterable is exhausted. Equivalent to:: |
| 460 | |
| 461 | def izip_longest(*args, **kwds): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 462 | # izip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D- |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 463 | fillvalue = kwds.get('fillvalue') |
| 464 | def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): |
| 465 | yield counter() # yields the fillvalue, or raises IndexError |
| 466 | fillers = repeat(fillvalue) |
| 467 | iters = [chain(it, sentinel(), fillers) for it in args] |
| 468 | try: |
| 469 | for tup in izip(*iters): |
| 470 | yield tup |
| 471 | except IndexError: |
| 472 | pass |
| 473 | |
Benjamin Peterson | 5255cba | 2008-07-25 17:02:11 +0000 | [diff] [blame] | 474 | If one of the iterables is potentially infinite, then the |
| 475 | :func:`izip_longest` function should be wrapped with something that limits |
| 476 | the number of calls (for example :func:`islice` or :func:`takewhile`). If |
| 477 | not specified, *fillvalue* defaults to ``None``. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 478 | |
| 479 | .. versionadded:: 2.6 |
| 480 | |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 481 | .. function:: permutations(iterable[, r]) |
| 482 | |
| 483 | Return successive *r* length permutations of elements in the *iterable*. |
| 484 | |
| 485 | If *r* is not specified or is ``None``, then *r* defaults to the length |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 486 | of the *iterable* and all possible full-length permutations |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 487 | are generated. |
| 488 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 489 | Permutations are emitted in lexicographic sort order. So, if the |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 490 | input *iterable* is sorted, the permutation tuples will be produced |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 491 | in sorted order. |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 492 | |
| 493 | Elements are treated as unique based on their position, not on their |
| 494 | value. So if the input elements are unique, there will be no repeat |
| 495 | values in each permutation. |
| 496 | |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 497 | Equivalent to:: |
| 498 | |
| 499 | def permutations(iterable, r=None): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 500 | # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC |
| 501 | # permutations(range(3)) --> 012 021 102 120 201 210 |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 502 | pool = tuple(iterable) |
| 503 | n = len(pool) |
| 504 | r = n if r is None else r |
Raymond Hettinger | 5b913e3 | 2009-01-08 06:39:04 +0000 | [diff] [blame] | 505 | if r > n: |
| 506 | return |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 507 | indices = range(n) |
Raymond Hettinger | e70bb8d | 2008-03-23 00:55:46 +0000 | [diff] [blame] | 508 | cycles = range(n, n-r, -1) |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 509 | yield tuple(pool[i] for i in indices[:r]) |
| 510 | while n: |
| 511 | for i in reversed(range(r)): |
| 512 | cycles[i] -= 1 |
| 513 | if cycles[i] == 0: |
Raymond Hettinger | 2b7a5c4 | 2008-03-02 11:17:51 +0000 | [diff] [blame] | 514 | indices[i:] = indices[i+1:] + indices[i:i+1] |
Raymond Hettinger | f287f17 | 2008-03-02 10:59:31 +0000 | [diff] [blame] | 515 | cycles[i] = n - i |
| 516 | else: |
| 517 | j = cycles[i] |
| 518 | indices[i], indices[-j] = indices[-j], indices[i] |
| 519 | yield tuple(pool[i] for i in indices[:r]) |
| 520 | break |
| 521 | else: |
| 522 | return |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 523 | |
Georg Brandl | c62ef8b | 2009-01-03 20:55:06 +0000 | [diff] [blame] | 524 | The code for :func:`permutations` can be also expressed as a subsequence of |
Raymond Hettinger | d553d85 | 2008-03-04 04:17:08 +0000 | [diff] [blame] | 525 | :func:`product`, filtered to exclude entries with repeated elements (those |
| 526 | from the same position in the input pool):: |
| 527 | |
| 528 | def permutations(iterable, r=None): |
| 529 | pool = tuple(iterable) |
| 530 | n = len(pool) |
| 531 | r = n if r is None else r |
| 532 | for indices in product(range(n), repeat=r): |
| 533 | if len(set(indices)) == r: |
| 534 | yield tuple(pool[i] for i in indices) |
| 535 | |
Raymond Hettinger | 5b913e3 | 2009-01-08 06:39:04 +0000 | [diff] [blame] | 536 | The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n`` |
| 537 | or zero when ``r > n``. |
| 538 | |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 539 | .. versionadded:: 2.6 |
| 540 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 541 | .. function:: product(*iterables[, repeat]) |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 542 | |
| 543 | Cartesian product of input iterables. |
| 544 | |
| 545 | Equivalent to nested for-loops in a generator expression. For example, |
| 546 | ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. |
| 547 | |
Raymond Hettinger | 5eaffc4 | 2008-04-17 10:48:31 +0000 | [diff] [blame] | 548 | The nested loops cycle like an odometer with the rightmost element advancing |
Andrew M. Kuchling | e2e0313 | 2008-04-17 20:44:06 +0000 | [diff] [blame] | 549 | on every iteration. This pattern creates a lexicographic ordering so that if |
| 550 | the input's iterables are sorted, the product tuples are emitted in sorted |
Raymond Hettinger | 5eaffc4 | 2008-04-17 10:48:31 +0000 | [diff] [blame] | 551 | order. |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 552 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 553 | To compute the product of an iterable with itself, specify the number of |
| 554 | repetitions with the optional *repeat* keyword argument. For example, |
| 555 | ``product(A, repeat=4)`` means the same as ``product(A, A, A, A)``. |
| 556 | |
Andrew M. Kuchling | 684868a | 2008-03-04 01:47:38 +0000 | [diff] [blame] | 557 | This function is equivalent to the following code, except that the |
| 558 | actual implementation does not build up intermediate results in memory:: |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 559 | |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 560 | def product(*args, **kwds): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 561 | # product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy |
| 562 | # product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111 |
Raymond Hettinger | 18750ab | 2008-02-28 09:23:48 +0000 | [diff] [blame] | 563 | pools = map(tuple, args) * kwds.get('repeat', 1) |
Raymond Hettinger | d553d85 | 2008-03-04 04:17:08 +0000 | [diff] [blame] | 564 | result = [[]] |
| 565 | for pool in pools: |
| 566 | result = [x+[y] for x in result for y in pool] |
| 567 | for prod in result: |
| 568 | yield tuple(prod) |
Raymond Hettinger | c5705a8 | 2008-02-22 19:50:06 +0000 | [diff] [blame] | 569 | |
| 570 | .. versionadded:: 2.6 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 571 | |
| 572 | .. function:: repeat(object[, times]) |
| 573 | |
| 574 | Make an iterator that returns *object* over and over again. Runs indefinitely |
| 575 | unless the *times* argument is specified. Used as argument to :func:`imap` for |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 576 | invariant function parameters. Also used with :func:`izip` to create constant |
| 577 | fields in a tuple record. Equivalent to:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 578 | |
| 579 | def repeat(object, times=None): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 580 | # repeat(10, 3) --> 10 10 10 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 581 | if times is None: |
| 582 | while True: |
| 583 | yield object |
| 584 | else: |
| 585 | for i in xrange(times): |
| 586 | yield object |
| 587 | |
| 588 | |
| 589 | .. function:: starmap(function, iterable) |
| 590 | |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 591 | Make an iterator that computes the function using arguments obtained from |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 592 | the iterable. Used instead of :func:`imap` when argument parameters are already |
| 593 | grouped in tuples from a single iterable (the data has been "pre-zipped"). The |
| 594 | difference between :func:`imap` and :func:`starmap` parallels the distinction |
| 595 | between ``function(a,b)`` and ``function(*c)``. Equivalent to:: |
| 596 | |
| 597 | def starmap(function, iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 598 | # starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000 |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 599 | for args in iterable: |
| 600 | yield function(*args) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 601 | |
Raymond Hettinger | 4731709 | 2008-01-17 03:02:14 +0000 | [diff] [blame] | 602 | .. versionchanged:: 2.6 |
| 603 | Previously, :func:`starmap` required the function arguments to be tuples. |
| 604 | Now, any iterable is allowed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 605 | |
| 606 | .. function:: takewhile(predicate, iterable) |
| 607 | |
| 608 | Make an iterator that returns elements from the iterable as long as the |
| 609 | predicate is true. Equivalent to:: |
| 610 | |
| 611 | def takewhile(predicate, iterable): |
Raymond Hettinger | 040f10e | 2008-03-06 01:15:52 +0000 | [diff] [blame] | 612 | # takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4 |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 613 | for x in iterable: |
| 614 | if predicate(x): |
| 615 | yield x |
| 616 | else: |
| 617 | break |
| 618 | |
| 619 | |
| 620 | .. function:: tee(iterable[, n=2]) |
| 621 | |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 622 | Return *n* independent iterators from a single iterable. Equivalent to:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 623 | |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 624 | def tee(iterable, n=2): |
| 625 | it = iter(iterable) |
| 626 | deques = [collections.deque() for i in range(n)] |
| 627 | def gen(mydeque): |
| 628 | while True: |
| 629 | if not mydeque: # when the local deque is empty |
| 630 | newval = next(it) # fetch a new value and |
| 631 | for d in deques: # load it to all the deques |
| 632 | d.append(newval) |
| 633 | yield mydeque.popleft() |
| 634 | return tuple(gen(d) for d in deques) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 635 | |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 636 | Once :func:`tee` has made a split, the original *iterable* should not be |
| 637 | used anywhere else; otherwise, the *iterable* could get advanced without |
| 638 | the tee objects being informed. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 639 | |
Raymond Hettinger | c8223b0 | 2009-02-18 20:54:53 +0000 | [diff] [blame] | 640 | This itertool may require significant auxiliary storage (depending on how |
| 641 | much temporary data needs to be stored). In general, if one iterator uses |
| 642 | most or all of the data before another iterator starts, it is faster to use |
| 643 | :func:`list` instead of :func:`tee`. |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 644 | |
| 645 | .. versionadded:: 2.4 |
| 646 | |
| 647 | |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 648 | .. _itertools-recipes: |
| 649 | |
| 650 | Recipes |
| 651 | ------- |
| 652 | |
| 653 | This section shows recipes for creating an extended toolset using the existing |
| 654 | itertools as building blocks. |
| 655 | |
| 656 | The extended tools offer the same high performance as the underlying toolset. |
| 657 | The superior memory performance is kept by processing elements one at a time |
| 658 | rather than bringing the whole iterable into memory all at once. Code volume is |
| 659 | kept small by linking the tools together in a functional style which helps |
| 660 | eliminate temporary variables. High speed is retained by preferring |
Georg Brandl | cf3fb25 | 2007-10-21 10:52:38 +0000 | [diff] [blame] | 661 | "vectorized" building blocks over the use of for-loops and :term:`generator`\s |
Georg Brandl | e8f1b00 | 2008-03-22 22:04:10 +0000 | [diff] [blame] | 662 | which incur interpreter overhead. |
| 663 | |
| 664 | .. testcode:: |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 665 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 666 | def take(n, iterable): |
| 667 | "Return first n items of the iterable as a list" |
| 668 | return list(islice(iterable, n)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 669 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 670 | def enumerate(iterable, start=0): |
| 671 | return izip(count(start), iterable) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 672 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 673 | def tabulate(function, start=0): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 674 | "Return function(0), function(1), ..." |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 675 | return imap(function, count(start)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 676 | |
Raymond Hettinger | 3496a89 | 2009-03-09 11:57:29 +0000 | [diff] [blame] | 677 | def consume(iterator, n): |
| 678 | "Advance the iterator n-steps ahead. If n is none, consume entirely." |
| 679 | collections.deque(islice(iterator, n), maxlen=0) |
| 680 | |
Raymond Hettinger | f9bce83 | 2009-02-19 05:34:35 +0000 | [diff] [blame] | 681 | def nth(iterable, n, default=None): |
| 682 | "Returns the nth item or a default value" |
| 683 | return next(islice(iterable, n, None), default) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 684 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 685 | def quantify(iterable, pred=bool): |
| 686 | "Count how many times the predicate is true" |
| 687 | return sum(imap(pred, iterable)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 688 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 689 | def padnone(iterable): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 690 | """Returns the sequence elements and then returns None indefinitely. |
| 691 | |
| 692 | Useful for emulating the behavior of the built-in map() function. |
| 693 | """ |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 694 | return chain(iterable, repeat(None)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 695 | |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 696 | def ncycles(iterable, n): |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 697 | "Returns the sequence elements n times" |
Raymond Hettinger | f1f46f0 | 2008-07-19 23:58:47 +0000 | [diff] [blame] | 698 | return chain.from_iterable(repeat(iterable, n)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 699 | |
| 700 | def dotproduct(vec1, vec2): |
| 701 | return sum(imap(operator.mul, vec1, vec2)) |
| 702 | |
| 703 | def flatten(listOfLists): |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 704 | return list(chain.from_iterable(listOfLists)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 705 | |
| 706 | def repeatfunc(func, times=None, *args): |
| 707 | """Repeat calls to func with specified arguments. |
| 708 | |
| 709 | Example: repeatfunc(random.random) |
| 710 | """ |
| 711 | if times is None: |
| 712 | return starmap(func, repeat(args)) |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 713 | return starmap(func, repeat(args, times)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 714 | |
| 715 | def pairwise(iterable): |
| 716 | "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 717 | a, b = tee(iterable) |
Raymond Hettinger | d47442e | 2009-02-23 19:32:55 +0000 | [diff] [blame] | 718 | next(b, None) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 719 | return izip(a, b) |
| 720 | |
Raymond Hettinger | 38fb9be | 2008-03-07 01:33:20 +0000 | [diff] [blame] | 721 | def grouper(n, iterable, fillvalue=None): |
Raymond Hettinger | efdf706 | 2008-07-30 07:27:30 +0000 | [diff] [blame] | 722 | "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx" |
Raymond Hettinger | 38fb9be | 2008-03-07 01:33:20 +0000 | [diff] [blame] | 723 | args = [iter(iterable)] * n |
Raymond Hettinger | f080e6d | 2008-07-31 01:19:50 +0000 | [diff] [blame] | 724 | return izip_longest(fillvalue=fillvalue, *args) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 725 | |
Raymond Hettinger | a44327a | 2008-01-30 22:17:31 +0000 | [diff] [blame] | 726 | def roundrobin(*iterables): |
Raymond Hettinger | efdf706 | 2008-07-30 07:27:30 +0000 | [diff] [blame] | 727 | "roundrobin('ABC', 'D', 'EF') --> A D E B F C" |
Raymond Hettinger | 330958e | 2008-02-28 19:41:24 +0000 | [diff] [blame] | 728 | # Recipe credited to George Sakkis |
Raymond Hettinger | a44327a | 2008-01-30 22:17:31 +0000 | [diff] [blame] | 729 | pending = len(iterables) |
| 730 | nexts = cycle(iter(it).next for it in iterables) |
| 731 | while pending: |
| 732 | try: |
| 733 | for next in nexts: |
| 734 | yield next() |
| 735 | except StopIteration: |
| 736 | pending -= 1 |
| 737 | nexts = cycle(islice(nexts, pending)) |
Georg Brandl | 8ec7f65 | 2007-08-15 14:28:01 +0000 | [diff] [blame] | 738 | |
Raymond Hettinger | 7832d4d | 2008-02-23 10:04:15 +0000 | [diff] [blame] | 739 | def powerset(iterable): |
Raymond Hettinger | 68d919e | 2009-01-25 21:31:47 +0000 | [diff] [blame] | 740 | "powerset([1,2,3]) --> () (1,) (2,) (3,) (1,2) (1,3) (2,3) (1,2,3)" |
| 741 | s = list(iterable) |
| 742 | return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) |
Raymond Hettinger | 7832d4d | 2008-02-23 10:04:15 +0000 | [diff] [blame] | 743 | |
Benjamin Peterson | 4829136 | 2009-01-31 20:01:48 +0000 | [diff] [blame] | 744 | def unique_everseen(iterable, key=None): |
| 745 | "List unique elements, preserving order. Remember all elements ever seen." |
| 746 | # unique_everseen('AAAABBBCCDAABBB') --> A B C D |
| 747 | # unique_everseen('ABBCcAD', str.lower) --> A B C D |
| 748 | seen = set() |
| 749 | seen_add = seen.add |
| 750 | if key is None: |
| 751 | for element in iterable: |
| 752 | if element not in seen: |
| 753 | seen_add(element) |
| 754 | yield element |
| 755 | else: |
| 756 | for element in iterable: |
| 757 | k = key(element) |
| 758 | if k not in seen: |
| 759 | seen_add(k) |
| 760 | yield element |
Raymond Hettinger | 44e1581 | 2009-01-02 21:26:45 +0000 | [diff] [blame] | 761 | |
Benjamin Peterson | 4829136 | 2009-01-31 20:01:48 +0000 | [diff] [blame] | 762 | def unique_justseen(iterable, key=None): |
| 763 | "List unique elements, preserving order. Remember only the element just seen." |
| 764 | # unique_justseen('AAAABBBCCDAABBB') --> A B C D A B |
| 765 | # unique_justseen('ABBCcAD', str.lower) --> A B C A D |
| 766 | return imap(next, imap(itemgetter(1), groupby(iterable, key))) |