Add recipe to docs.

This commit is contained in:
Raymond Hettinger 2008-03-11 00:19:07 +00:00
parent 0098c9d609
commit e8b4b60555
2 changed files with 14 additions and 0 deletions

View File

@ -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

View File

@ -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}