]> granicus.if.org Git - python/commitdiff
Add recipe to docs.
authorRaymond Hettinger <python@rcn.com>
Tue, 11 Mar 2008 00:19:07 +0000 (00:19 +0000)
committerRaymond Hettinger <python@rcn.com>
Tue, 11 Mar 2008 00:19:07 +0000 (00:19 +0000)
Doc/library/itertools.rst
Lib/test/test_itertools.py

index f546fe16ee2bf55ac5c5f725891722f9bc41979a..1f67739e643d1d02c519e0c4045053b4979a4a48 100644 (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
index 696fdebf1ef028256f822011e7468f13b685ca8d..3b5cc23909321a16883b44c5ee09b57fb82b20c7 100644 (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}