Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit f5a2e47

Browse files
committed
Neaten-up the itertools recipes.
1 parent fbf66bd commit f5a2e47

2 files changed

Lines changed: 8 additions & 12 deletions

File tree

Doc/library/itertools.rst

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -563,12 +563,12 @@ which incur interpreter overhead.
563563
return zip(a, b)
564564

565565
def grouper(n, iterable, fillvalue=None):
566-
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
566+
"grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
567567
args = [iter(iterable)] * n
568568
return zip_longest(*args, fillvalue=fillvalue)
569569
570570
def roundrobin(*iterables):
571-
"roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
571+
"roundrobin('ABC', 'D', 'EF') --> A D E B F C"
572572
# Recipe credited to George Sakkis
573573
pending = len(iterables)
574574
nexts = cycle(iter(it).__next__ for it in iterables)
@@ -588,10 +588,8 @@ which incur interpreter overhead.
588588
yield set(x for m, x in pairs if m&n)
589589

590590
def compress(data, selectors):
591-
"compress('abcdef', [1,0,1,0,1,1]) --> a c e f"
592-
decorated = zip(data, selectors)
593-
filtered = filter(operator.itemgetter(1), decorated)
594-
return map(operator.itemgetter(0), filtered)
591+
"compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
592+
return (d for d, s in izip(data, selectors) if s)
595593

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

Lib/test/test_itertools.py

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1255,13 +1255,13 @@ def __init__(self, newarg=None, *args):
12551255
... return zip(a, b)
12561256
12571257
>>> def grouper(n, iterable, fillvalue=None):
1258-
... "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
1258+
... "grouper(3, 'ABCDEFG', 'x') --> ABC DEF Gxx"
12591259
... args = [iter(iterable)] * n
12601260
... kwds = dict(fillvalue=fillvalue)
12611261
... return zip_longest(*args, **kwds)
12621262
12631263
>>> def roundrobin(*iterables):
1264-
... "roundrobin('abc', 'd', 'ef') --> 'a', 'd', 'e', 'b', 'f', 'c'"
1264+
... "roundrobin('ABC', 'D', 'EF') --> A D E B F C"
12651265
... # Recipe credited to George Sakkis
12661266
... pending = len(iterables)
12671267
... nexts = cycle(iter(it).__next__ for it in iterables)
@@ -1281,10 +1281,8 @@ def __init__(self, newarg=None, *args):
12811281
... yield set(x for m, x in pairs if m&n)
12821282
12831283
>>> def compress(data, selectors):
1284-
... "compress('abcdef', [1,0,1,0,1,1]) --> a c e f"
1285-
... decorated = zip(data, selectors)
1286-
... filtered = filter(operator.itemgetter(1), decorated)
1287-
... return map(operator.itemgetter(0), filtered)
1284+
... "compress('ABCDEF', [1,0,1,0,1,1]) --> A C E F"
1285+
... return (d for d, s in zip(data, selectors) if s)
12881286
12891287
>>> def combinations_with_replacement(iterable, r):
12901288
... "combinations_with_replacement('ABC', 3) --> AA AB AC BB BC CC"

0 commit comments

Comments
 (0)