# [Python-3000-checkins] r65297 - in python/branches/py3k: Doc/library/itertools.rst Lib/test/test_itertools.py

raymond.hettinger python-3000-checkins at python.org
Wed Jul 30 09:37:38 CEST 2008

```Author: raymond.hettinger
Date: Wed Jul 30 09:37:37 2008
New Revision: 65297

Log:
Neaten-up the itertools recipes.

Modified:
python/branches/py3k/Doc/library/itertools.rst
python/branches/py3k/Lib/test/test_itertools.py

Modified: python/branches/py3k/Doc/library/itertools.rst
==============================================================================
--- python/branches/py3k/Doc/library/itertools.rst	(original)
+++ python/branches/py3k/Doc/library/itertools.rst	Wed Jul 30 09:37:37 2008
@@ -563,12 +563,12 @@
return zip(a, b)

def grouper(n, iterable, fillvalue=None):
-       "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
+       "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
args = [iter(iterable)] * n
return zip_longest(*args, fillvalue=fillvalue)

def roundrobin(*iterables):
-       "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
+       "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
# Recipe credited to George Sakkis
pending = len(iterables)
nexts = cycle(iter(it).__next__ for it in iterables)
@@ -588,10 +588,8 @@
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"
-       decorated = zip(data, selectors)
-       filtered =  filter(operator.itemgetter(1), decorated)
-       return map(operator.itemgetter(0), filtered)
+       "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
+       return (d for d, s in izip(data, selectors) if s)

def combinations_with_replacement(iterable, r):
"combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"

Modified: python/branches/py3k/Lib/test/test_itertools.py
==============================================================================
--- python/branches/py3k/Lib/test/test_itertools.py	(original)
+++ python/branches/py3k/Lib/test/test_itertools.py	Wed Jul 30 09:37:37 2008
@@ -1255,13 +1255,13 @@
...     return zip(a, b)

>>> def grouper(n, iterable, fillvalue=None):
-...     "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
+...     "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
...     args = [iter(iterable)] * n
...     kwds = dict(fillvalue=fillvalue)
...     return zip_longest(*args, **kwds)

>>> def roundrobin(*iterables):
-...     "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
+...     "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
...     # Recipe credited to George Sakkis
...     pending = len(iterables)
...     nexts = cycle(iter(it).__next__ for it in iterables)
@@ -1281,10 +1281,8 @@
...         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"
-...     decorated = zip(data, selectors)
-...     filtered =  filter(operator.itemgetter(1), decorated)
-...     return map(operator.itemgetter(0), filtered)
+...     "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
+...     return (d for d, s in zip(data, selectors) if s)

>>> def combinations_with_replacement(iterable, r):
...     "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"
```