Skip to content

Commit

Permalink
Neaten-up the itertools recipes.
Browse files Browse the repository at this point in the history
  • Loading branch information
rhettinger committed Jul 30, 2008
1 parent fbf66bd commit f5a2e47
Show file tree
Hide file tree
Showing 2 changed files with 8 additions and 12 deletions.
10 changes: 4 additions & 6 deletions Doc/library/itertools.rst
Original file line number Diff line number Diff line change
Expand Up @@ -563,12 +563,12 @@ which incur interpreter overhead.
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)
Expand All @@ -588,10 +588,8 @@ which incur interpreter overhead.
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"
Expand Down
10 changes: 4 additions & 6 deletions Lib/test/test_itertools.py
Original file line number Diff line number Diff line change
Expand Up @@ -1255,13 +1255,13 @@ def __init__(self, newarg=None, *args):
... 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)
Expand All @@ -1281,10 +1281,8 @@ def __init__(self, newarg=None, *args):
... 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"
Expand Down

0 comments on commit f5a2e47

Please sign in to comment.