diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:56:58 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-26 02:56:58 (GMT) |
commit | 6b3b0fc4d48549b8ee2b5df3409229de80430040 (patch) | |
tree | a0fb0a7d9a00e584e1403ea515e809e56615c539 /Doc/library/itertools.rst | |
parent | ace673391acbe0411b20b972b6068dab7a0bbdb4 (diff) | |
download | cpython-6b3b0fc4d48549b8ee2b5df3409229de80430040.zip cpython-6b3b0fc4d48549b8ee2b5df3409229de80430040.tar.gz cpython-6b3b0fc4d48549b8ee2b5df3409229de80430040.tar.bz2 |
Forward port r68941 adding itertools.compress().
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 36254cd..d281278 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -133,6 +133,20 @@ loops that truncate the stream. The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n`` or zero when ``r > n``. +.. 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 zip(data, selectors) if s) + + .. versionadded:: 2.7 + + .. function:: count([n]) Make an iterator that returns consecutive integers starting with *n*. If not @@ -594,10 +608,6 @@ which incur interpreter overhead. s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) - def compress(data, selectors): - "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F" - return (d for d, s in zip(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)! |