Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 1 | \section{\module{itertools} --- |
| 2 | Functions creating iterators for efficient looping} |
| 3 | |
| 4 | \declaremodule{standard}{itertools} |
| 5 | \modulesynopsis{Functions creating iterators for efficient looping.} |
| 6 | \moduleauthor{Raymond Hettinger}{python@rcn.com} |
| 7 | \sectionauthor{Raymond Hettinger}{python@rcn.com} |
| 8 | \versionadded{2.3} |
| 9 | |
| 10 | |
| 11 | This module implements a number of iterator building blocks inspired |
| 12 | by constructs from the Haskell and SML programming languages. Each |
| 13 | has been recast in a form suitable for Python. |
| 14 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 15 | The module standardizes a core set of fast, memory efficient tools |
| 16 | that are useful by themselves or in combination. Standardization helps |
| 17 | avoid the readability and reliability problems which arise when many |
| 18 | different individuals create their own slightly varying implementations, |
| 19 | each with their own quirks and naming conventions. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 20 | |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 21 | The tools are designed to combine readily with one another. This makes |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 22 | it easy to construct more specialized tools succinctly and efficiently |
| 23 | in pure Python. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 24 | |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 25 | For instance, SML provides a tabulation tool: \code{tabulate(f)} |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 26 | which produces a sequence \code{f(0), f(1), ...}. This toolbox |
| 27 | provides \function{imap()} and \function{count()} which can be combined |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 28 | to form \code{imap(f, count())} and produce an equivalent result. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 29 | |
Raymond Hettinger | 863983e | 2003-04-23 00:09:42 +0000 | [diff] [blame] | 30 | Likewise, the functional tools are designed to work well with the |
| 31 | high-speed functions provided by the \refmodule{operator} module. |
| 32 | |
| 33 | The module author welcomes suggestions for other basic building blocks |
| 34 | to be added to future versions of the module. |
| 35 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 36 | Whether cast in pure python form or C code, tools that use iterators |
| 37 | are more memory efficient (and faster) than their list based counterparts. |
| 38 | Adopting the principles of just-in-time manufacturing, they create |
| 39 | data when and where needed instead of consuming memory with the |
| 40 | computer equivalent of ``inventory''. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 41 | |
Raymond Hettinger | 863983e | 2003-04-23 00:09:42 +0000 | [diff] [blame] | 42 | The performance advantage of iterators becomes more acute as the number |
| 43 | of elements increases -- at some point, lists grow large enough to |
Raymond Hettinger | 7e43110 | 2003-09-22 15:00:55 +0000 | [diff] [blame] | 44 | severely impact memory cache performance and start running slowly. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 45 | |
| 46 | \begin{seealso} |
| 47 | \seetext{The Standard ML Basis Library, |
| 48 | \citetitle[http://www.standardml.org/Basis/] |
| 49 | {The Standard ML Basis Library}.} |
| 50 | |
| 51 | \seetext{Haskell, A Purely Functional Language, |
| 52 | \citetitle[http://www.haskell.org/definition/] |
| 53 | {Definition of Haskell and the Standard Libraries}.} |
| 54 | \end{seealso} |
| 55 | |
| 56 | |
| 57 | \subsection{Itertool functions \label{itertools-functions}} |
| 58 | |
| 59 | The following module functions all construct and return iterators. |
| 60 | Some provide streams of infinite length, so they should only be accessed |
| 61 | by functions or loops that truncate the stream. |
| 62 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 63 | \begin{funcdesc}{chain}{*iterables} |
| 64 | Make an iterator that returns elements from the first iterable until |
| 65 | it is exhausted, then proceeds to the next iterable, until all of the |
| 66 | iterables are exhausted. Used for treating consecutive sequences as |
| 67 | a single sequence. Equivalent to: |
| 68 | |
| 69 | \begin{verbatim} |
| 70 | def chain(*iterables): |
| 71 | for it in iterables: |
| 72 | for element in it: |
| 73 | yield element |
| 74 | \end{verbatim} |
| 75 | \end{funcdesc} |
| 76 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 77 | \begin{funcdesc}{count}{\optional{n}} |
| 78 | Make an iterator that returns consecutive integers starting with \var{n}. |
| 79 | Does not currently support python long integers. Often used as an |
| 80 | argument to \function{imap()} to generate consecutive data points. |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 81 | Also, used with \function{izip()} to add sequence numbers. Equivalent to: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 82 | |
| 83 | \begin{verbatim} |
| 84 | def count(n=0): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 85 | while True: |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 86 | yield n |
| 87 | n += 1 |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 88 | \end{verbatim} |
Raymond Hettinger | 2012f17 | 2003-02-07 05:32:58 +0000 | [diff] [blame] | 89 | |
| 90 | Note, \function{count()} does not check for overflow and will return |
| 91 | negative numbers after exceeding \code{sys.maxint}. This behavior |
| 92 | may change in the future. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 93 | \end{funcdesc} |
| 94 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 95 | \begin{funcdesc}{cycle}{iterable} |
| 96 | Make an iterator returning elements from the iterable and saving a |
| 97 | copy of each. When the iterable is exhausted, return elements from |
| 98 | the saved copy. Repeats indefinitely. Equivalent to: |
| 99 | |
| 100 | \begin{verbatim} |
| 101 | def cycle(iterable): |
| 102 | saved = [] |
| 103 | for element in iterable: |
| 104 | yield element |
| 105 | saved.append(element) |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 106 | while saved: |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 107 | for element in saved: |
| 108 | yield element |
| 109 | \end{verbatim} |
| 110 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 111 | Note, this member of the toolkit may require significant |
| 112 | auxiliary storage (depending on the length of the iterable). |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 113 | \end{funcdesc} |
| 114 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 115 | \begin{funcdesc}{dropwhile}{predicate, iterable} |
| 116 | Make an iterator that drops elements from the iterable as long as |
| 117 | the predicate is true; afterwards, returns every element. Note, |
| 118 | the iterator does not produce \emph{any} output until the predicate |
| 119 | is true, so it may have a lengthy start-up time. Equivalent to: |
| 120 | |
| 121 | \begin{verbatim} |
| 122 | def dropwhile(predicate, iterable): |
| 123 | iterable = iter(iterable) |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 124 | for x in iterable: |
| 125 | if not predicate(x): |
| 126 | yield x |
| 127 | break |
| 128 | for x in iterable: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 129 | yield x |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 130 | \end{verbatim} |
| 131 | \end{funcdesc} |
| 132 | |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 133 | \begin{funcdesc}{ifilter}{predicate, iterable} |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 134 | Make an iterator that filters elements from iterable returning only |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 135 | those for which the predicate is \code{True}. |
| 136 | If \var{predicate} is \code{None}, return the items that are true. |
| 137 | Equivalent to: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 138 | |
| 139 | \begin{verbatim} |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 140 | def ifilter(predicate, iterable): |
| 141 | if predicate is None: |
Guido van Rossum | 0c9a318 | 2003-10-20 17:01:07 +0000 | [diff] [blame] | 142 | predicate = bool |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 143 | for x in iterable: |
| 144 | if predicate(x): |
| 145 | yield x |
| 146 | \end{verbatim} |
| 147 | \end{funcdesc} |
| 148 | |
| 149 | \begin{funcdesc}{ifilterfalse}{predicate, iterable} |
| 150 | Make an iterator that filters elements from iterable returning only |
| 151 | those for which the predicate is \code{False}. |
| 152 | If \var{predicate} is \code{None}, return the items that are false. |
| 153 | Equivalent to: |
| 154 | |
| 155 | \begin{verbatim} |
| 156 | def ifilterfalse(predicate, iterable): |
| 157 | if predicate is None: |
Guido van Rossum | 0c9a318 | 2003-10-20 17:01:07 +0000 | [diff] [blame] | 158 | predicate = bool |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 159 | for x in iterable: |
| 160 | if not predicate(x): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 161 | yield x |
| 162 | \end{verbatim} |
| 163 | \end{funcdesc} |
| 164 | |
| 165 | \begin{funcdesc}{imap}{function, *iterables} |
| 166 | Make an iterator that computes the function using arguments from |
| 167 | each of the iterables. If \var{function} is set to \code{None}, then |
| 168 | \function{imap()} returns the arguments as a tuple. Like |
| 169 | \function{map()} but stops when the shortest iterable is exhausted |
| 170 | instead of filling in \code{None} for shorter iterables. The reason |
| 171 | for the difference is that infinite iterator arguments are typically |
| 172 | an error for \function{map()} (because the output is fully evaluated) |
| 173 | but represent a common and useful way of supplying arguments to |
| 174 | \function{imap()}. |
| 175 | Equivalent to: |
| 176 | |
| 177 | \begin{verbatim} |
| 178 | def imap(function, *iterables): |
| 179 | iterables = map(iter, iterables) |
| 180 | while True: |
| 181 | args = [i.next() for i in iterables] |
| 182 | if function is None: |
| 183 | yield tuple(args) |
| 184 | else: |
| 185 | yield function(*args) |
| 186 | \end{verbatim} |
| 187 | \end{funcdesc} |
| 188 | |
| 189 | \begin{funcdesc}{islice}{iterable, \optional{start,} stop \optional{, step}} |
| 190 | Make an iterator that returns selected elements from the iterable. |
| 191 | If \var{start} is non-zero, then elements from the iterable are skipped |
| 192 | until start is reached. Afterward, elements are returned consecutively |
| 193 | unless \var{step} is set higher than one which results in items being |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 194 | skipped. If \var{stop} is \code{None}, then iteration continues until |
| 195 | the iterator is exhausted, if at all; otherwise, it stops at the specified |
| 196 | position. Unlike regular slicing, |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 197 | \function{islice()} does not support negative values for \var{start}, |
| 198 | \var{stop}, or \var{step}. Can be used to extract related fields |
| 199 | from data where the internal structure has been flattened (for |
| 200 | example, a multi-line report may list a name field on every |
| 201 | third line). Equivalent to: |
| 202 | |
| 203 | \begin{verbatim} |
| 204 | def islice(iterable, *args): |
Raymond Hettinger | 341deb7 | 2003-05-02 19:44:20 +0000 | [diff] [blame] | 205 | s = slice(*args) |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 206 | next, stop, step = s.start or 0, s.stop, s.step or 1 |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 207 | for cnt, element in enumerate(iterable): |
| 208 | if cnt < next: |
| 209 | continue |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 210 | if stop is not None and cnt >= stop: |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 211 | break |
| 212 | yield element |
Raymond Hettinger | 14ef54c | 2003-05-02 19:04:37 +0000 | [diff] [blame] | 213 | next += step |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 214 | \end{verbatim} |
| 215 | \end{funcdesc} |
| 216 | |
| 217 | \begin{funcdesc}{izip}{*iterables} |
| 218 | Make an iterator that aggregates elements from each of the iterables. |
| 219 | Like \function{zip()} except that it returns an iterator instead of |
| 220 | a list. Used for lock-step iteration over several iterables at a |
| 221 | time. Equivalent to: |
| 222 | |
| 223 | \begin{verbatim} |
| 224 | def izip(*iterables): |
| 225 | iterables = map(iter, iterables) |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 226 | while iterables: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 227 | result = [i.next() for i in iterables] |
| 228 | yield tuple(result) |
| 229 | \end{verbatim} |
Raymond Hettinger | b5a4208 | 2003-08-08 05:10:41 +0000 | [diff] [blame] | 230 | |
| 231 | \versionchanged[When no iterables are specified, returns a zero length |
| 232 | iterator instead of raising a TypeError exception]{2.4} |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 233 | \end{funcdesc} |
| 234 | |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 235 | \begin{funcdesc}{repeat}{object\optional{, times}} |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 236 | Make an iterator that returns \var{object} over and over again. |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 237 | Runs indefinitely unless the \var{times} argument is specified. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 238 | Used as argument to \function{imap()} for invariant parameters |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 239 | to the called function. Also used with \function{izip()} to create |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 240 | an invariant part of a tuple record. Equivalent to: |
| 241 | |
| 242 | \begin{verbatim} |
Raymond Hettinger | 61fe64d | 2003-02-23 04:40:07 +0000 | [diff] [blame] | 243 | def repeat(object, times=None): |
| 244 | if times is None: |
| 245 | while True: |
| 246 | yield object |
| 247 | else: |
| 248 | for i in xrange(times): |
| 249 | yield object |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 250 | \end{verbatim} |
| 251 | \end{funcdesc} |
| 252 | |
| 253 | \begin{funcdesc}{starmap}{function, iterable} |
| 254 | Make an iterator that computes the function using arguments tuples |
| 255 | obtained from the iterable. Used instead of \function{imap()} when |
| 256 | argument parameters are already grouped in tuples from a single iterable |
| 257 | (the data has been ``pre-zipped''). The difference between |
Raymond Hettinger | 1b18ba4 | 2003-02-21 01:45:34 +0000 | [diff] [blame] | 258 | \function{imap()} and \function{starmap()} parallels the distinction |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 259 | between \code{function(a,b)} and \code{function(*c)}. |
| 260 | Equivalent to: |
| 261 | |
| 262 | \begin{verbatim} |
| 263 | def starmap(function, iterable): |
| 264 | iterable = iter(iterable) |
| 265 | while True: |
| 266 | yield function(*iterable.next()) |
| 267 | \end{verbatim} |
| 268 | \end{funcdesc} |
| 269 | |
| 270 | \begin{funcdesc}{takewhile}{predicate, iterable} |
| 271 | Make an iterator that returns elements from the iterable as long as |
| 272 | the predicate is true. Equivalent to: |
| 273 | |
| 274 | \begin{verbatim} |
| 275 | def takewhile(predicate, iterable): |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 276 | for x in iterable: |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 277 | if predicate(x): |
| 278 | yield x |
| 279 | else: |
| 280 | break |
| 281 | \end{verbatim} |
| 282 | \end{funcdesc} |
| 283 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 284 | \begin{funcdesc}{tee}{iterable\optional{, n=2}} |
| 285 | Return \var{n} independent iterators from a single iterable. |
| 286 | The case where \var{n} is two is equivalent to: |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 287 | |
| 288 | \begin{verbatim} |
| 289 | def tee(iterable): |
| 290 | def gen(next, data={}, cnt=[0]): |
| 291 | for i in count(): |
| 292 | if i == cnt[0]: |
| 293 | item = data[i] = next() |
| 294 | cnt[0] += 1 |
| 295 | else: |
| 296 | item = data.pop(i) |
| 297 | yield item |
| 298 | it = iter(iterable) |
| 299 | return (gen(it.next), gen(it.next)) |
| 300 | \end{verbatim} |
| 301 | |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 302 | Note, once \function{tee()} has made a split, the original \var{iterable} |
| 303 | should not be used anywhere else; otherwise, the \var{iterable} could get |
| 304 | advanced without the tee objects being informed. |
| 305 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 306 | Note, this member of the toolkit may require significant auxiliary |
| 307 | storage (depending on how much temporary data needs to be stored). |
| 308 | In general, if one iterator is going use most or all of the data before |
| 309 | the other iterator, it is faster to use \function{list()} instead of |
| 310 | \function{tee()}. |
| 311 | \versionadded{2.4} |
| 312 | \end{funcdesc} |
| 313 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 314 | |
| 315 | \subsection{Examples \label{itertools-example}} |
| 316 | |
| 317 | The following examples show common uses for each tool and |
| 318 | demonstrate ways they can be combined. |
| 319 | |
| 320 | \begin{verbatim} |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 321 | |
| 322 | >>> amounts = [120.15, 764.05, 823.14] |
| 323 | >>> for checknum, amount in izip(count(1200), amounts): |
| 324 | ... print 'Check %d is for $%.2f' % (checknum, amount) |
| 325 | ... |
| 326 | Check 1200 is for $120.15 |
| 327 | Check 1201 is for $764.05 |
| 328 | Check 1202 is for $823.14 |
| 329 | |
| 330 | >>> import operator |
| 331 | >>> for cube in imap(operator.pow, xrange(1,4), repeat(3)): |
| 332 | ... print cube |
| 333 | ... |
| 334 | 1 |
| 335 | 8 |
| 336 | 27 |
| 337 | |
| 338 | >>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura', |
| 339 | '', 'martin', '', 'walter', '', 'samuele'] |
Raymond Hettinger | 3567a87 | 2003-06-28 05:44:36 +0000 | [diff] [blame] | 340 | >>> for name in islice(reportlines, 3, None, 2): |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 341 | ... print name.title() |
| 342 | ... |
| 343 | Alex |
| 344 | Laura |
| 345 | Martin |
| 346 | Walter |
| 347 | Samuele |
| 348 | |
| 349 | \end{verbatim} |
| 350 | |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 351 | This section shows how itertools can be combined to create other more |
| 352 | powerful itertools. Note that \function{enumerate()} and \method{iteritems()} |
| 353 | already have efficient implementations in Python. They are only included here |
| 354 | to illustrate how higher level tools can be created from building blocks. |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 355 | |
| 356 | \begin{verbatim} |
Raymond Hettinger | a098b33 | 2003-09-08 23:58:40 +0000 | [diff] [blame] | 357 | def take(n, seq): |
| 358 | return list(islice(seq, n)) |
| 359 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 360 | def enumerate(iterable): |
| 361 | return izip(count(), iterable) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 362 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 363 | def tabulate(function): |
| 364 | "Return function(0), function(1), ..." |
| 365 | return imap(function, count()) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 366 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 367 | def iteritems(mapping): |
| 368 | return izip(mapping.iterkeys(), mapping.itervalues()) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 369 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 370 | def nth(iterable, n): |
| 371 | "Returns the nth item" |
| 372 | return list(islice(iterable, n, n+1)) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 373 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 374 | def all(seq, pred=bool): |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 375 | "Returns True if pred(x) is True for every element in the iterable" |
| 376 | return False not in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 377 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 378 | def any(seq, pred=bool): |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 379 | "Returns True if pred(x) is True at least one element in the iterable" |
| 380 | return True in imap(pred, seq) |
Raymond Hettinger | 60eca93 | 2003-02-09 06:40:58 +0000 | [diff] [blame] | 381 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 382 | def no(seq, pred=bool): |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 383 | "Returns True if pred(x) is False for every element in the iterable" |
| 384 | return True not in imap(pred, seq) |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 385 | |
Raymond Hettinger | dbe3d28 | 2003-10-05 16:47:36 +0000 | [diff] [blame] | 386 | def quantify(seq, pred=bool): |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 387 | "Count how many times the predicate is True in the sequence" |
| 388 | return sum(imap(pred, seq)) |
Raymond Hettinger | c7d7766 | 2003-08-08 02:40:28 +0000 | [diff] [blame] | 389 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 390 | def padnone(seq): |
| 391 | "Returns the sequence elements and then returns None indefinitely" |
| 392 | return chain(seq, repeat(None)) |
Raymond Hettinger | 863983e | 2003-04-23 00:09:42 +0000 | [diff] [blame] | 393 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 394 | def ncycles(seq, n): |
| 395 | "Returns the sequence elements n times" |
| 396 | return chain(*repeat(seq, n)) |
Raymond Hettinger | 863983e | 2003-04-23 00:09:42 +0000 | [diff] [blame] | 397 | |
Raymond Hettinger | 9e38641 | 2003-08-25 05:06:09 +0000 | [diff] [blame] | 398 | def dotproduct(vec1, vec2): |
| 399 | return sum(imap(operator.mul, vec1, vec2)) |
Raymond Hettinger | 863983e | 2003-04-23 00:09:42 +0000 | [diff] [blame] | 400 | |
Raymond Hettinger | 6a5b027 | 2003-10-24 08:45:23 +0000 | [diff] [blame] | 401 | def flatten(listOfLists): |
| 402 | return list(chain(*listOfLists)) |
| 403 | |
| 404 | def repeatfunc(func, times=None, *args): |
| 405 | "Repeat calls to func with specified arguments." |
| 406 | "Example: repeatfunc(random.random)" |
| 407 | if times is None: |
| 408 | return starmap(func, repeat(args)) |
| 409 | else: |
| 410 | return starmap(func, repeat(args, times)) |
| 411 | |
Raymond Hettinger | d591f66 | 2003-10-26 15:34:50 +0000 | [diff] [blame] | 412 | def pairwise(iterable): |
| 413 | "s -> (s0,s1), (s1,s2), (s2, s3), ..." |
| 414 | a, b = tee(iterable) |
Raymond Hettinger | ad983e7 | 2003-11-12 14:32:26 +0000 | [diff] [blame] | 415 | try: |
| 416 | b.next() |
| 417 | except StopIteration: |
| 418 | pass |
| 419 | return izip(a, b) |
Raymond Hettinger | befa37d | 2003-06-18 19:25:37 +0000 | [diff] [blame] | 420 | |
Raymond Hettinger | 96ef811 | 2003-02-01 00:10:11 +0000 | [diff] [blame] | 421 | \end{verbatim} |