From bc9b090f0d81c03bd5bb9e5897e5acd73eda59e3 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. git-svn-id: http://svn.python.org/projects/python/trunk@61344 6015fed2-1504-0410-9fe1-9d1591cc4771 --- 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 f546fe16ee..1f67739e64 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 696fdebf1e..3b5cc23909 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} -- 2.11.4.GIT