Kaydet (Commit) efdf706a authored tarafından Raymond Hettinger's avatar Raymond Hettinger

Neaten-up the itertools recipes.

üst 66b14de7
......@@ -645,13 +645,13 @@ which incur interpreter overhead.
return izip(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 izip_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)
......@@ -671,10 +671,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 = izip(data, selectors)
filtered = ifilter(operator.itemgetter(1), decorated)
return imap(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"
......
......@@ -1234,13 +1234,13 @@ Samuele
... return izip(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 izip_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)
......@@ -1260,10 +1260,8 @@ Samuele
... 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 = izip(data, selectors)
... filtered = ifilter(operator.itemgetter(1), decorated)
... return imap(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"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment