From 39e0eb766f02e18711d7eac9f948754a1ee569e3 Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Sat, 19 Jul 2008 23:21:57 +0000 Subject: [PATCH] Fix compress() recipe in docs to use itertools. --- Doc/library/itertools.rst | 6 +++--- 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 9a3626f98cd4..d8c33316b450 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 82e1ee43e8e3..2d07d8df0c71 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" -- 2.47.3