diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-25 21:04:14 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-25 21:04:14 (GMT) |
commit | 2bcb8e9b0d19bf60b38553dcb1d9a6c4cb86a8a1 (patch) | |
tree | 11bc79db1c0995c2a116f55abf8d704b4234c2aa /Doc/library/itertools.rst | |
parent | 1af3b5a9a42217959868d9510d80c5a805af5fca (diff) | |
download | cpython-2bcb8e9b0d19bf60b38553dcb1d9a6c4cb86a8a1.zip cpython-2bcb8e9b0d19bf60b38553dcb1d9a6c4cb86a8a1.tar.gz cpython-2bcb8e9b0d19bf60b38553dcb1d9a6c4cb86a8a1.tar.bz2 |
Promote compress() from a recipe to being a regular itertool.
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r-- | Doc/library/itertools.rst | 18 |
1 files changed, 14 insertions, 4 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index aef3f6a..903284e 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -139,6 +139,20 @@ loops that truncate the stream. .. versionadded:: 2.6 +.. function:: compress(data, selectors) + + Make an iterator that filters elements from *data* returning only those that + have a corresponding element in *selectors* that evaluates to ``True``. + Stops when either the *data* or *selectors* iterables have been exhausted. + Equivalent to:: + + def compress(data, selectors): + # compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F + return (d for d, s in izip(data, selectors) if s) + + .. versionadded:: 2.7 + + .. function:: count([n]) Make an iterator that returns consecutive integers starting with *n*. If not @@ -679,10 +693,6 @@ which incur interpreter overhead. for n in xrange(2**len(pairs)): yield set(x for m, x in pairs if m&n) - def compress(data, selectors): - "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" - return (d for d, s in izip(data, selectors) if s) - def combinations_with_replacement(iterable, r): "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC" # number items returned: (n+r-1)! / r! / (n-1)! |