diff options
author | Raymond Hettinger <python@rcn.com> | 2008-07-19 23:21:57 +0000 |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2008-07-19 23:21:57 +0000 |
commit | 39e0eb766f02e18711d7eac9f948754a1ee569e3 (patch) | |
tree | 434853b0d4bc508925d85fc55a88f48b0357e587 | |
parent | d648f64a530a77db93df89cc03306ef80b27ff4f (diff) | |
download | cpython-git-39e0eb766f02e18711d7eac9f948754a1ee569e3.tar.gz |
Fix compress() recipe in docs to use itertools.
-rw-r--r-- | Doc/library/itertools.rst | 6 | ||||
-rw-r--r-- | Lib/test/test_itertools.py | 6 |
2 files changed, 6 insertions, 6 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 9a3626f98c..d8c33316b4 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -698,9 +698,9 @@ which incur interpreter overhead. def compress(data, selectors): "compress('abcdef', [1,0,1,0,1,1]) --> a c e f" - for d, s in izip(data, selectors): - if s: - yield d + decorated = izip(data, selectors) + filtered = ifilter(operator.itemgetter(1), decorated) + return imap(operator.itemgetter(0), filtered) def combinations_with_replacement(iterable, r): "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC" diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 82e1ee43e8..2d07d8df0c 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1281,9 +1281,9 @@ Samuele >>> def compress(data, selectors): ... "compress('abcdef', [1,0,1,0,1,1]) --> a c e f" -... for d, s in izip(data, selectors): -... if s: -... yield d +... decorated = izip(data, selectors) +... filtered = ifilter(operator.itemgetter(1), decorated) +... return imap(operator.itemgetter(0), filtered) >>> def combinations_with_replacement(iterable, r): ... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC" |