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

Sync-up with doc improvements in Py2.6

üst 736c0ab4
...@@ -1104,20 +1104,30 @@ available. They are listed here in alphabetical order. ...@@ -1104,20 +1104,30 @@ available. They are listed here in alphabetical order.
the effects on the corresponding symbol table are undefined. [#]_ the effects on the corresponding symbol table are undefined. [#]_
.. function:: zip([iterable, ...]) .. function:: zip(*iterables)
This function returns an iterator of tuples, where the *i*-th tuple contains Make an iterator that aggregates elements from each of the iterables.
Returns an iterator of tuples, where the *i*-th tuple contains
the *i*-th element from each of the argument sequences or iterables. The the *i*-th element from each of the argument sequences or iterables. The
iterator stops when the shortest argument sequence is exhausted. When there iterator stops when the shortest input iterable is exhausted. With a single
are multiple arguments which are all of the same length, :func:`zip` is iterable argument, it returns an iterator of 1-tuples. With no arguments,
similar to :func:`map` with an initial argument of ``None``. With a single it returns an empty iterator. Equivalent to::
sequence argument, it returns an iterator of 1-tuples. With no arguments, it
returns an empty iterator. def zip(*iterables):
# zip('ABCD', 'xy') --> Ax By
iterables = map(iter, iterables)
while iterables:
result = [it.next() for it in iterables]
yield tuple(result)
The left-to-right evaluation order of the iterables is guaranteed. This The left-to-right evaluation order of the iterables is guaranteed. This
makes possible an idiom for clustering a data series into n-length groups makes possible an idiom for clustering a data series into n-length groups
using ``zip(*[iter(s)]*n)``. using ``zip(*[iter(s)]*n)``.
:func:`zip` should only be used with unequal length inputs when you don't
care about trailing, unmatched values from the longer iterables. If those
values are important, use :func:`itertools.zip_longest` instead.
.. rubric:: Footnotes .. rubric:: Footnotes
......
...@@ -68,6 +68,7 @@ loops that truncate the stream. ...@@ -68,6 +68,7 @@ loops that truncate the stream.
Equivalent to:: Equivalent to::
def chain(*iterables): def chain(*iterables):
# chain('ABC', 'DEF') --> A B C D E F
for it in iterables: for it in iterables:
for element in it: for element in it:
yield element yield element
...@@ -80,6 +81,7 @@ loops that truncate the stream. ...@@ -80,6 +81,7 @@ loops that truncate the stream.
@classmethod @classmethod
def from_iterable(iterables): def from_iterable(iterables):
# chain.from_iterable(['ABC', 'DEF']) --> A B C D E F
for it in iterables: for it in iterables:
for element in it: for element in it:
yield element yield element
...@@ -105,7 +107,8 @@ loops that truncate the stream. ...@@ -105,7 +107,8 @@ loops that truncate the stream.
Equivalent to:: Equivalent to::
def combinations(iterable, r): def combinations(iterable, r):
'combinations(range(4), 3) --> (0,1,2) (0,1,3) (0,2,3) (1,2,3)' # combinations('ABCD', 2) --> AB AC AD BC BD CD
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable) pool = tuple(iterable)
n = len(pool) n = len(pool)
indices = range(r) indices = range(r)
...@@ -138,10 +141,11 @@ loops that truncate the stream. ...@@ -138,10 +141,11 @@ loops that truncate the stream.
Make an iterator that returns consecutive integers starting with *n*. If not Make an iterator that returns consecutive integers starting with *n*. If not
specified *n* defaults to zero. Often used as an argument to :func:`map` to specified *n* defaults to zero. Often used as an argument to :func:`map` to
generate consecutive data points. Also, used with :func:`izip` to add sequence generate consecutive data points. Also, used with :func:`zip` to add sequence
numbers. Equivalent to:: numbers. Equivalent to::
def count(n=0): def count(n=0):
# count(10) --> 10 11 12 13 14 ...
while True: while True:
yield n yield n
n += 1 n += 1
...@@ -154,6 +158,7 @@ loops that truncate the stream. ...@@ -154,6 +158,7 @@ loops that truncate the stream.
indefinitely. Equivalent to:: indefinitely. Equivalent to::
def cycle(iterable): def cycle(iterable):
# cycle('ABCD') --> A B C D A B C D A B C D ...
saved = [] saved = []
for element in iterable: for element in iterable:
yield element yield element
...@@ -174,6 +179,7 @@ loops that truncate the stream. ...@@ -174,6 +179,7 @@ loops that truncate the stream.
start-up time. Equivalent to:: start-up time. Equivalent to::
def dropwhile(predicate, iterable): def dropwhile(predicate, iterable):
# dropwhile(lambda x: x<5, [1,4,6,4,1]) --> 6 4 1
iterable = iter(iterable) iterable = iter(iterable)
for x in iterable: for x in iterable:
if not predicate(x): if not predicate(x):
...@@ -212,6 +218,8 @@ loops that truncate the stream. ...@@ -212,6 +218,8 @@ loops that truncate the stream.
:func:`groupby` is equivalent to:: :func:`groupby` is equivalent to::
class groupby(object): class groupby(object):
# [k for k, g in groupby('AAAABBBCCDAABBB')] --> A B C D A B
# [(list(g)) for k, g in groupby('AAAABBBCCD')] --> AAAA BBB CC D
def __init__(self, iterable, key=None): def __init__(self, iterable, key=None):
if key is None: if key is None:
key = lambda x: x key = lambda x: x
...@@ -240,6 +248,7 @@ loops that truncate the stream. ...@@ -240,6 +248,7 @@ loops that truncate the stream.
that are false. Equivalent to:: that are false. Equivalent to::
def filterfalse(predicate, iterable): def filterfalse(predicate, iterable):
# filterfalse(lambda x: x%2, range(10)) --> 0 2 4 6 8
if predicate is None: if predicate is None:
predicate = bool predicate = bool
for x in iterable: for x in iterable:
...@@ -260,6 +269,10 @@ loops that truncate the stream. ...@@ -260,6 +269,10 @@ loops that truncate the stream.
multi-line report may list a name field on every third line). Equivalent to:: multi-line report may list a name field on every third line). Equivalent to::
def islice(iterable, *args): def islice(iterable, *args):
# islice('ABCDEFG', 2) --> A B
# islice('ABCDEFG', 2, 4) --> C D
# islice('ABCDEFG', 2, None) --> C D E F G
# islice('ABCDEFG', 0, None, 2) --> A C E G
s = slice(*args) s = slice(*args)
it = range(s.start or 0, s.stop or sys.maxsize, s.step or 1) it = range(s.start or 0, s.stop or sys.maxsize, s.step or 1)
nexti = next(it) nexti = next(it)
...@@ -272,29 +285,6 @@ loops that truncate the stream. ...@@ -272,29 +285,6 @@ loops that truncate the stream.
then the step defaults to one. then the step defaults to one.
.. function:: izip(*iterables)
Make an iterator that aggregates elements from each of the iterables. Like
:func:`zip` except that it returns an iterator instead of a list. Used for
lock-step iteration over several iterables at a time. Equivalent to::
def izip(*iterables):
iterables = map(iter, iterables)
while iterables:
result = [next(it) for it in iterables]
yield tuple(result)
When no iterables are specified, return a zero length iterator.
The left-to-right evaluation order of the iterables is guaranteed. This
makes possible an idiom for clustering a data series into n-length groups
using ``izip(*[iter(s)]*n)``.
:func:`izip` should only be used with unequal length inputs when you don't
care about trailing, unmatched values from the longer iterables. If those
values are important, use :func:`zip_longest` instead.
.. function:: zip_longest(*iterables[, fillvalue]) .. function:: zip_longest(*iterables[, fillvalue])
Make an iterator that aggregates elements from each of the iterables. If the Make an iterator that aggregates elements from each of the iterables. If the
...@@ -302,13 +292,14 @@ loops that truncate the stream. ...@@ -302,13 +292,14 @@ loops that truncate the stream.
Iteration continues until the longest iterable is exhausted. Equivalent to:: Iteration continues until the longest iterable is exhausted. Equivalent to::
def zip_longest(*args, **kwds): def zip_longest(*args, **kwds):
# zip_longest('ABCD', 'xy', fillvalue='-') --> Ax By C- D-
fillvalue = kwds.get('fillvalue') fillvalue = kwds.get('fillvalue')
def sentinel(counter = ([fillvalue]*(len(args)-1)).pop): def sentinel(counter = ([fillvalue]*(len(args)-1)).pop):
yield counter() # yields the fillvalue, or raises IndexError yield counter() # yields the fillvalue, or raises IndexError
fillers = repeat(fillvalue) fillers = repeat(fillvalue)
iters = [chain(it, sentinel(), fillers) for it in args] iters = [chain(it, sentinel(), fillers) for it in args]
try: try:
for tup in izip(*iters): for tup in zip(*iters):
yield tup yield tup
except IndexError: except IndexError:
pass pass
...@@ -337,7 +328,8 @@ loops that truncate the stream. ...@@ -337,7 +328,8 @@ loops that truncate the stream.
Equivalent to:: Equivalent to::
def permutations(iterable, r=None): def permutations(iterable, r=None):
'permutations(range(3), 2) --> (0,1) (0,2) (1,0) (1,2) (2,0) (2,1)' # permutations('ABCD', 2) --> AB AC AD BA BC BD CA CB CD DA DB DC
# permutations(range(3)) --> 012 021 102 120 201 210
pool = tuple(iterable) pool = tuple(iterable)
n = len(pool) n = len(pool)
r = n if r is None else r r = n if r is None else r
...@@ -379,8 +371,8 @@ loops that truncate the stream. ...@@ -379,8 +371,8 @@ loops that truncate the stream.
Equivalent to nested for-loops in a generator expression. For example, Equivalent to nested for-loops in a generator expression. For example,
``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``. ``product(A, B)`` returns the same as ``((x,y) for x in A for y in B)``.
The leftmost iterators are in the outermost for-loop, so the output tuples The leftmost iterators correspond to the outermost for-loop, so the output
cycle like an odometer (with the rightmost element changing on every tuples cycle like an odometer (with the rightmost element changing on every
iteration). This results in a lexicographic ordering so that if the iteration). This results in a lexicographic ordering so that if the
inputs iterables are sorted, the product tuples are emitted inputs iterables are sorted, the product tuples are emitted
in sorted order. in sorted order.
...@@ -393,6 +385,8 @@ loops that truncate the stream. ...@@ -393,6 +385,8 @@ loops that truncate the stream.
actual implementation does not build up intermediate results in memory:: actual implementation does not build up intermediate results in memory::
def product(*args, **kwds): def product(*args, **kwds):
# product('ABCD', 'xy') --> Ax Ay Bx By Cx Cy Dx Dy
# product(range(2), repeat=3) --> 000 001 010 011 100 101 110 111
pools = map(tuple, args) * kwds.get('repeat', 1) pools = map(tuple, args) * kwds.get('repeat', 1)
result = [[]] result = [[]]
for pool in pools: for pool in pools:
...@@ -405,10 +399,11 @@ loops that truncate the stream. ...@@ -405,10 +399,11 @@ loops that truncate the stream.
Make an iterator that returns *object* over and over again. Runs indefinitely Make an iterator that returns *object* over and over again. Runs indefinitely
unless the *times* argument is specified. Used as argument to :func:`map` for unless the *times* argument is specified. Used as argument to :func:`map` for
invariant parameters to the called function. Also used with :func:`izip` to invariant parameters to the called function. Also used with :func:`zip` to
create an invariant part of a tuple record. Equivalent to:: create an invariant part of a tuple record. Equivalent to::
def repeat(object, times=None): def repeat(object, times=None):
# repeat(10, 3) --> 10 10 10
if times is None: if times is None:
while True: while True:
yield object yield object
...@@ -426,6 +421,7 @@ loops that truncate the stream. ...@@ -426,6 +421,7 @@ loops that truncate the stream.
between ``function(a,b)`` and ``function(*c)``. Equivalent to:: between ``function(a,b)`` and ``function(*c)``. Equivalent to::
def starmap(function, iterable): def starmap(function, iterable):
# starmap(pow, [(2,5), (3,2), (10,3)]) --> 32 9 1000
for args in iterable: for args in iterable:
yield function(*args) yield function(*args)
...@@ -440,6 +436,7 @@ loops that truncate the stream. ...@@ -440,6 +436,7 @@ loops that truncate the stream.
predicate is true. Equivalent to:: predicate is true. Equivalent to::
def takewhile(predicate, iterable): def takewhile(predicate, iterable):
# takewhile(lambda x: x<5, [1,4,6,4,1]) --> 1 4
for x in iterable: for x in iterable:
if predicate(x): if predicate(x):
yield x yield x
...@@ -481,34 +478,6 @@ Examples ...@@ -481,34 +478,6 @@ Examples
The following examples show common uses for each tool and demonstrate ways they The following examples show common uses for each tool and demonstrate ways they
can be combined. :: can be combined. ::
>>> amounts = [120.15, 764.05, 823.14]
>>> for checknum, amount in izip(count(1200), amounts):
... print('Check %d is for $%.2f' % (checknum, amount))
...
Check 1200 is for $120.15
Check 1201 is for $764.05
Check 1202 is for $823.14
>>> import operator
>>> for cube in map(operator.pow, range(1,5), repeat(3)):
... print(cube)
...
1
8
27
64
>>> reportlines = ['EuroPython', 'Roster', '', 'alex', '', 'laura',
... '', 'martin', '', 'walter', '', 'mark']
>>> for name in islice(reportlines, 3, None, 2):
... print(name.title())
...
Alex
Laura
Martin
Walter
Mark
# Show a dictionary sorted and grouped by value # Show a dictionary sorted and grouped by value
>>> from operator import itemgetter >>> from operator import itemgetter
>>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3) >>> d = dict(a=1, b=2, c=1, d=2, e=1, f=2, g=3)
...@@ -556,15 +525,18 @@ which incur interpreter overhead. :: ...@@ -556,15 +525,18 @@ which incur interpreter overhead. ::
return list(islice(seq, n)) return list(islice(seq, n))
def enumerate(iterable): def enumerate(iterable):
return izip(count(), iterable) return zip(count(), iterable)
def tabulate(function): def tabulate(function):
"Return function(0), function(1), ..." "Return function(0), function(1), ..."
return map(function, count()) return map(function, count())
def items(mapping):
return zip(mapping.keys(), mapping.values())
def nth(iterable, n): def nth(iterable, n):
"Returns the nth item or raise StopIteration" "Returns the nth item or raise StopIteration"
return islice(iterable, n, None).next() return next(islice(iterable, n, None))
def all(seq, pred=None): def all(seq, pred=None):
"Returns True if pred(x) is true for every element in the iterable" "Returns True if pred(x) is true for every element in the iterable"
...@@ -617,18 +589,21 @@ which incur interpreter overhead. :: ...@@ -617,18 +589,21 @@ which incur interpreter overhead. ::
def pairwise(iterable): def pairwise(iterable):
"s -> (s0,s1), (s1,s2), (s2, s3), ..." "s -> (s0,s1), (s1,s2), (s2, s3), ..."
a, b = tee(iterable) a, b = tee(iterable)
next(b, None) for elem in b:
return izip(a, b) break
return zip(a, b)
def grouper(n, iterable, padvalue=None): def grouper(n, iterable, fillvalue=None):
"grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')" "grouper(3, 'abcdefg', 'x') --> ('a','b','c'), ('d','e','f'), ('g','x','x')"
return izip(*[chain(iterable, repeat(padvalue, n-1))]*n) args = [iter(iterable)] * n
kwds = dict(fillvalue=fillvalue)
return zip_longest(*args, **kwds)
def roundrobin(*iterables): 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 # Recipe credited to George Sakkis
pending = len(iterables) pending = len(iterables)
nexts = cycle(iter(it).next for it in iterables) nexts = cycle(iter(it).__next__ for it in iterables)
while pending: while pending:
try: try:
for next in nexts: for next in nexts:
...@@ -644,3 +619,9 @@ which incur interpreter overhead. :: ...@@ -644,3 +619,9 @@ which incur interpreter overhead. ::
for n in xrange(2**len(pairs)): for n in xrange(2**len(pairs)):
yield set(x for m, x in pairs if m&n) 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"
for d, s in zip(data, selectors):
if s:
yield d
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