From e8b4b605550441a17b8b2db3b9e3391b8752ce8d Mon Sep 17 00:00:00 2001 From: Raymond Hettinger Date: Tue, 11 Mar 2008 00:19:07 +0000 Subject: [PATCH] Add recipe to docs. --- Doc/library/itertools.rst | 5 +++++ Lib/test/test_itertools.py | 9 +++++++++ 2 files changed, 14 insertions(+) diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index f546fe16ee2..1f67739e643 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -692,3 +692,8 @@ 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" + for d, s in izip(data, selectors): + if s: + yield d diff --git a/Lib/test/test_itertools.py b/Lib/test/test_itertools.py index 696fdebf1ef..3b5cc239093 100644 --- a/Lib/test/test_itertools.py +++ b/Lib/test/test_itertools.py @@ -1279,6 +1279,12 @@ Samuele ... 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" +... for d, s in izip(data, selectors): +... if s: +... yield d + This is not part of the examples but it tests to make sure the definitions perform as purported. @@ -1353,6 +1359,9 @@ False >>> map(sorted, powerset('ab')) [[], ['a'], ['b'], ['a', 'b']] +>>> list(compress('abcdef', [1,0,1,0,1,1])) +['a', 'c', 'e', 'f'] + """ __test__ = {'libreftest' : libreftest}