]> granicus.if.org Git - python/commitdiff
Fix compress() recipe in docs to use itertools.
authorRaymond Hettinger <python@rcn.com>
Sat, 19 Jul 2008 23:21:57 +0000 (23:21 +0000)
committerRaymond Hettinger <python@rcn.com>
Sat, 19 Jul 2008 23:21:57 +0000 (23:21 +0000)
Doc/library/itertools.rst
Lib/test/test_itertools.py

index 9a3626f98cd4ffc68e371d09a366e422c74a2b79..d8c33316b45038c06cf2eed60eb5e6f68da9c39a 100644 (file)
@@ -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"
index 82e1ee43e8e3af108e777728b87760595109904f..2d07d8df0c71120e62f5a6ef5ff1695617ac1553 100644 (file)
@@ -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"