mirror of
https://github.com/python/cpython.git
synced 2025-12-04 16:43:27 +00:00
Misc cleanups and wording improvements for the itertools docs (gh-119626)
This commit is contained in:
parent
3ff06ebec4
commit
0bd0d4072a
1 changed files with 116 additions and 122 deletions
|
|
@ -56,13 +56,13 @@ Iterator Arguments Results
|
||||||
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F``
|
:func:`chain` p, q, ... p0, p1, ... plast, q0, q1, ... ``chain('ABC', 'DEF') → A B C D E F``
|
||||||
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
|
:func:`chain.from_iterable` iterable p0, p1, ... plast, q0, q1, ... ``chain.from_iterable(['ABC', 'DEF']) → A B C D E F``
|
||||||
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
|
:func:`compress` data, selectors (d[0] if s[0]), (d[1] if s[1]), ... ``compress('ABCDEF', [1,0,1,0,1,1]) → A C E F``
|
||||||
:func:`dropwhile` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,4,1]) → 6 4 1``
|
:func:`dropwhile` predicate, seq seq[n], seq[n+1], starting when predicate fails ``dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8``
|
||||||
:func:`filterfalse` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x%2, range(10)) → 0 2 4 6 8``
|
:func:`filterfalse` predicate, seq elements of seq where predicate(elem) fails ``filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8``
|
||||||
:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v)
|
:func:`groupby` iterable[, key] sub-iterators grouped by value of key(v)
|
||||||
:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) → C D E F G``
|
:func:`islice` seq, [start,] stop [, step] elements from seq[start:stop:step] ``islice('ABCDEFG', 2, None) → C D E F G``
|
||||||
:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') → AB BC CD DE EF FG``
|
:func:`pairwise` iterable (p[0], p[1]), (p[1], p[2]) ``pairwise('ABCDEFG') → AB BC CD DE EF FG``
|
||||||
:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000``
|
:func:`starmap` func, seq func(\*seq[0]), func(\*seq[1]), ... ``starmap(pow, [(2,5), (3,2), (10,3)]) → 32 9 1000``
|
||||||
:func:`takewhile` predicate, seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,4,1]) → 1 4``
|
:func:`takewhile` predicate, seq seq[0], seq[1], until predicate fails ``takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4``
|
||||||
:func:`tee` it, n it1, it2, ... itn splits one iterator into n
|
:func:`tee` it, n it1, it2, ... itn splits one iterator into n
|
||||||
:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-``
|
:func:`zip_longest` p, q, ... (p[0], q[0]), (p[1], q[1]), ... ``zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-``
|
||||||
============================ ============================ ================================================= =============================================================
|
============================ ============================ ================================================= =============================================================
|
||||||
|
|
@ -97,31 +97,27 @@ The following module functions all construct and return iterators. Some provide
|
||||||
streams of infinite length, so they should only be accessed by functions or
|
streams of infinite length, so they should only be accessed by functions or
|
||||||
loops that truncate the stream.
|
loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: accumulate(iterable[, func, *, initial=None])
|
|
||||||
|
|
||||||
Make an iterator that returns accumulated sums, or accumulated
|
.. function:: accumulate(iterable[, function, *, initial=None])
|
||||||
results of other binary functions (specified via the optional
|
|
||||||
*func* argument).
|
|
||||||
|
|
||||||
If *func* is supplied, it should be a function
|
Make an iterator that returns accumulated sums or accumulated
|
||||||
of two arguments. Elements of the input *iterable* may be any type
|
results from other binary functions.
|
||||||
that can be accepted as arguments to *func*. (For example, with
|
|
||||||
the default operation of addition, elements may be any addable
|
|
||||||
type including :class:`~decimal.Decimal` or
|
|
||||||
:class:`~fractions.Fraction`.)
|
|
||||||
|
|
||||||
Usually, the number of elements output matches the input iterable.
|
The *function* defaults to addition. The *function* should accept
|
||||||
However, if the keyword argument *initial* is provided, the
|
two arguments, an accumulated total and a value from the *iterable*.
|
||||||
accumulation leads off with the *initial* value so that the output
|
|
||||||
has one more element than the input iterable.
|
If an *initial* value is provided, the accumulation will start with
|
||||||
|
that value and the output will have one more element than the input
|
||||||
|
iterable.
|
||||||
|
|
||||||
Roughly equivalent to::
|
Roughly equivalent to::
|
||||||
|
|
||||||
def accumulate(iterable, func=operator.add, *, initial=None):
|
def accumulate(iterable, function=operator.add, *, initial=None):
|
||||||
'Return running totals'
|
'Return running totals'
|
||||||
# accumulate([1,2,3,4,5]) → 1 3 6 10 15
|
# accumulate([1,2,3,4,5]) → 1 3 6 10 15
|
||||||
# accumulate([1,2,3,4,5], initial=100) → 100 101 103 106 110 115
|
# accumulate([1,2,3,4,5], initial=100) → 100 101 103 106 110 115
|
||||||
# accumulate([1,2,3,4,5], operator.mul) → 1 2 6 24 120
|
# accumulate([1,2,3,4,5], operator.mul) → 1 2 6 24 120
|
||||||
|
|
||||||
iterator = iter(iterable)
|
iterator = iter(iterable)
|
||||||
total = initial
|
total = initial
|
||||||
if initial is None:
|
if initial is None:
|
||||||
|
|
@ -129,27 +125,29 @@ loops that truncate the stream.
|
||||||
total = next(iterator)
|
total = next(iterator)
|
||||||
except StopIteration:
|
except StopIteration:
|
||||||
return
|
return
|
||||||
|
|
||||||
yield total
|
yield total
|
||||||
for element in iterator:
|
for element in iterator:
|
||||||
total = func(total, element)
|
total = function(total, element)
|
||||||
yield total
|
yield total
|
||||||
|
|
||||||
The *func* argument can be set to
|
The *function* argument can be set to :func:`min` for a running
|
||||||
:func:`min` for a running minimum, :func:`max` for a running maximum, or
|
minimum, :func:`max` for a running maximum, or :func:`operator.mul`
|
||||||
:func:`operator.mul` for a running product. Amortization tables can be
|
for a running product. `Amortization tables
|
||||||
built by accumulating interest and applying payments:
|
<https://www.ramseysolutions.com/real-estate/amortization-schedule>`_
|
||||||
|
can be built by accumulating interest and applying payments:
|
||||||
|
|
||||||
.. doctest::
|
.. doctest::
|
||||||
|
|
||||||
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
|
>>> data = [3, 4, 6, 2, 1, 9, 0, 7, 5, 8]
|
||||||
>>> list(accumulate(data, operator.mul)) # running product
|
|
||||||
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
|
|
||||||
>>> list(accumulate(data, max)) # running maximum
|
>>> list(accumulate(data, max)) # running maximum
|
||||||
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
|
[3, 4, 6, 6, 6, 9, 9, 9, 9, 9]
|
||||||
|
>>> list(accumulate(data, operator.mul)) # running product
|
||||||
|
[3, 12, 72, 144, 144, 1296, 0, 0, 0, 0]
|
||||||
|
|
||||||
# Amortize a 5% loan of 1000 with 10 annual payments of 90
|
# Amortize a 5% loan of 1000 with 10 annual payments of 90
|
||||||
>>> account_update = lambda bal, pmt: round(bal * 1.05) + pmt
|
>>> update = lambda balance, payment: round(balance * 1.05) - payment
|
||||||
>>> list(accumulate(repeat(-90, 10), account_update, initial=1_000))
|
>>> list(accumulate(repeat(90, 10), update, initial=1_000))
|
||||||
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
|
[1000, 960, 918, 874, 828, 779, 728, 674, 618, 559, 497]
|
||||||
|
|
||||||
See :func:`functools.reduce` for a similar function that returns only the
|
See :func:`functools.reduce` for a similar function that returns only the
|
||||||
|
|
@ -158,7 +156,7 @@ loops that truncate the stream.
|
||||||
.. versionadded:: 3.2
|
.. versionadded:: 3.2
|
||||||
|
|
||||||
.. versionchanged:: 3.3
|
.. versionchanged:: 3.3
|
||||||
Added the optional *func* parameter.
|
Added the optional *function* parameter.
|
||||||
|
|
||||||
.. versionchanged:: 3.8
|
.. versionchanged:: 3.8
|
||||||
Added the optional *initial* parameter.
|
Added the optional *initial* parameter.
|
||||||
|
|
@ -190,8 +188,8 @@ loops that truncate the stream.
|
||||||
# batched('ABCDEFG', 3) → ABC DEF G
|
# batched('ABCDEFG', 3) → ABC DEF G
|
||||||
if n < 1:
|
if n < 1:
|
||||||
raise ValueError('n must be at least one')
|
raise ValueError('n must be at least one')
|
||||||
iterable = iter(iterable)
|
iterator = iter(iterable)
|
||||||
while batch := tuple(islice(iterable, n)):
|
while batch := tuple(islice(iterator, n)):
|
||||||
if strict and len(batch) != n:
|
if strict and len(batch) != n:
|
||||||
raise ValueError('batched(): incomplete batch')
|
raise ValueError('batched(): incomplete batch')
|
||||||
yield batch
|
yield batch
|
||||||
|
|
@ -230,12 +228,17 @@ loops that truncate the stream.
|
||||||
|
|
||||||
Return *r* length subsequences of elements from the input *iterable*.
|
Return *r* length subsequences of elements from the input *iterable*.
|
||||||
|
|
||||||
|
The output is a subsequence of :func:`product` keeping only entries that
|
||||||
|
are subsequences of the *iterable*. The length of the output is given
|
||||||
|
by :func:`math.comb` which computes ``n! / r! / (n - r)!`` when ``0 ≤ r
|
||||||
|
≤ n`` or zero when ``r > n``.
|
||||||
|
|
||||||
The combination tuples are emitted in lexicographic order according to
|
The combination tuples are emitted in lexicographic order according to
|
||||||
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
the order of the input *iterable*. If the input *iterable* is sorted,
|
||||||
the output tuples will be produced in sorted order.
|
the output tuples will be produced in sorted order.
|
||||||
|
|
||||||
Elements are treated as unique based on their position, not on their
|
Elements are treated as unique based on their position, not on their
|
||||||
value. So, if the input elements are unique, there will be no repeated
|
value. If the input elements are unique, there will be no repeated
|
||||||
values within each combination.
|
values within each combination.
|
||||||
|
|
||||||
Roughly equivalent to::
|
Roughly equivalent to::
|
||||||
|
|
@ -243,11 +246,13 @@ loops that truncate the stream.
|
||||||
def combinations(iterable, r):
|
def combinations(iterable, r):
|
||||||
# combinations('ABCD', 2) → AB AC AD BC BD CD
|
# combinations('ABCD', 2) → AB AC AD BC BD CD
|
||||||
# combinations(range(4), 3) → 012 013 023 123
|
# combinations(range(4), 3) → 012 013 023 123
|
||||||
|
|
||||||
pool = tuple(iterable)
|
pool = tuple(iterable)
|
||||||
n = len(pool)
|
n = len(pool)
|
||||||
if r > n:
|
if r > n:
|
||||||
return
|
return
|
||||||
indices = list(range(r))
|
indices = list(range(r))
|
||||||
|
|
||||||
yield tuple(pool[i] for i in indices)
|
yield tuple(pool[i] for i in indices)
|
||||||
while True:
|
while True:
|
||||||
for i in reversed(range(r)):
|
for i in reversed(range(r)):
|
||||||
|
|
@ -260,42 +265,36 @@ loops that truncate the stream.
|
||||||
indices[j] = indices[j-1] + 1
|
indices[j] = indices[j-1] + 1
|
||||||
yield tuple(pool[i] for i in indices)
|
yield tuple(pool[i] for i in indices)
|
||||||
|
|
||||||
The code for :func:`combinations` can be also expressed as a subsequence
|
|
||||||
of :func:`permutations` after filtering entries where the elements are not
|
|
||||||
in sorted order (according to their position in the input pool)::
|
|
||||||
|
|
||||||
def combinations(iterable, r):
|
|
||||||
pool = tuple(iterable)
|
|
||||||
n = len(pool)
|
|
||||||
for indices in permutations(range(n), r):
|
|
||||||
if sorted(indices) == list(indices):
|
|
||||||
yield tuple(pool[i] for i in indices)
|
|
||||||
|
|
||||||
The number of items returned is ``n! / r! / (n-r)!`` when ``0 <= r <= n``
|
|
||||||
or zero when ``r > n``.
|
|
||||||
|
|
||||||
.. function:: combinations_with_replacement(iterable, r)
|
.. function:: combinations_with_replacement(iterable, r)
|
||||||
|
|
||||||
Return *r* length subsequences of elements from the input *iterable*
|
Return *r* length subsequences of elements from the input *iterable*
|
||||||
allowing individual elements to be repeated more than once.
|
allowing individual elements to be repeated more than once.
|
||||||
|
|
||||||
|
The output is a subsequence of :func:`product` that keeps only entries
|
||||||
|
that are subsequences (with possible repeated elements) of the
|
||||||
|
*iterable*. The number of subsequence returned is ``(n + r - 1)! / r! /
|
||||||
|
(n - 1)!`` when ``n > 0``.
|
||||||
|
|
||||||
The combination tuples are emitted in lexicographic order according to
|
The combination tuples are emitted in lexicographic order according to
|
||||||
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
the order of the input *iterable*. if the input *iterable* is sorted,
|
||||||
the output tuples will be produced in sorted order.
|
the output tuples will be produced in sorted order.
|
||||||
|
|
||||||
Elements are treated as unique based on their position, not on their
|
Elements are treated as unique based on their position, not on their
|
||||||
value. So, if the input elements are unique, the generated combinations
|
value. If the input elements are unique, the generated combinations
|
||||||
will also be unique.
|
will also be unique.
|
||||||
|
|
||||||
Roughly equivalent to::
|
Roughly equivalent to::
|
||||||
|
|
||||||
def combinations_with_replacement(iterable, r):
|
def combinations_with_replacement(iterable, r):
|
||||||
# combinations_with_replacement('ABC', 2) → AA AB AC BB BC CC
|
# combinations_with_replacement('ABC', 2) → AA AB AC BB BC CC
|
||||||
|
|
||||||
pool = tuple(iterable)
|
pool = tuple(iterable)
|
||||||
n = len(pool)
|
n = len(pool)
|
||||||
if not n and r:
|
if not n and r:
|
||||||
return
|
return
|
||||||
indices = [0] * r
|
indices = [0] * r
|
||||||
|
|
||||||
yield tuple(pool[i] for i in indices)
|
yield tuple(pool[i] for i in indices)
|
||||||
while True:
|
while True:
|
||||||
for i in reversed(range(r)):
|
for i in reversed(range(r)):
|
||||||
|
|
@ -306,28 +305,15 @@ loops that truncate the stream.
|
||||||
indices[i:] = [indices[i] + 1] * (r - i)
|
indices[i:] = [indices[i] + 1] * (r - i)
|
||||||
yield tuple(pool[i] for i in indices)
|
yield tuple(pool[i] for i in indices)
|
||||||
|
|
||||||
The code for :func:`combinations_with_replacement` can be also expressed as
|
|
||||||
a subsequence of :func:`product` after filtering entries where the elements
|
|
||||||
are not in sorted order (according to their position in the input pool)::
|
|
||||||
|
|
||||||
def combinations_with_replacement(iterable, r):
|
|
||||||
pool = tuple(iterable)
|
|
||||||
n = len(pool)
|
|
||||||
for indices in product(range(n), repeat=r):
|
|
||||||
if sorted(indices) == list(indices):
|
|
||||||
yield tuple(pool[i] for i in indices)
|
|
||||||
|
|
||||||
The number of items returned is ``(n+r-1)! / r! / (n-1)!`` when ``n > 0``.
|
|
||||||
|
|
||||||
.. versionadded:: 3.1
|
.. versionadded:: 3.1
|
||||||
|
|
||||||
|
|
||||||
.. function:: compress(data, selectors)
|
.. function:: compress(data, selectors)
|
||||||
|
|
||||||
Make an iterator that filters elements from *data* returning only those that
|
Make an iterator that returns elements from *data* where the
|
||||||
have a corresponding element in *selectors* is true.
|
corresponding element in *selectors* is true. Stops when either the
|
||||||
Stops when either the *data* or *selectors* iterables have been exhausted.
|
*data* or *selectors* iterables have been exhausted. Roughly
|
||||||
Roughly equivalent to::
|
equivalent to::
|
||||||
|
|
||||||
def compress(data, selectors):
|
def compress(data, selectors):
|
||||||
# compress('ABCDEF', [1,0,1,0,1,1]) → A C E F
|
# compress('ABCDEF', [1,0,1,0,1,1]) → A C E F
|
||||||
|
|
@ -338,9 +324,10 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: count(start=0, step=1)
|
.. function:: count(start=0, step=1)
|
||||||
|
|
||||||
Make an iterator that returns evenly spaced values starting with number *start*. Often
|
Make an iterator that returns evenly spaced values beginning with
|
||||||
used as an argument to :func:`map` to generate consecutive data points.
|
*start*. Can be used with :func:`map` to generate consecutive data
|
||||||
Also, used with :func:`zip` to add sequence numbers. Roughly equivalent to::
|
points or with :func:`zip` to add sequence numbers. Roughly
|
||||||
|
equivalent to::
|
||||||
|
|
||||||
def count(start=0, step=1):
|
def count(start=0, step=1):
|
||||||
# count(10) → 10 11 12 13 14 ...
|
# count(10) → 10 11 12 13 14 ...
|
||||||
|
|
@ -357,11 +344,12 @@ loops that truncate the stream.
|
||||||
.. versionchanged:: 3.1
|
.. versionchanged:: 3.1
|
||||||
Added *step* argument and allowed non-integer arguments.
|
Added *step* argument and allowed non-integer arguments.
|
||||||
|
|
||||||
|
|
||||||
.. function:: cycle(iterable)
|
.. function:: cycle(iterable)
|
||||||
|
|
||||||
Make an iterator returning elements from the iterable and saving a copy of each.
|
Make an iterator returning elements from the *iterable* and saving a
|
||||||
When the iterable is exhausted, return elements from the saved copy. Repeats
|
copy of each. When the iterable is exhausted, return elements from
|
||||||
indefinitely. Roughly equivalent to::
|
the saved copy. Repeats indefinitely. Roughly equivalent to::
|
||||||
|
|
||||||
def cycle(iterable):
|
def cycle(iterable):
|
||||||
# cycle('ABCD') → A B C D A B C D A B C D ...
|
# cycle('ABCD') → A B C D A B C D A B C D ...
|
||||||
|
|
@ -373,32 +361,38 @@ loops that truncate the stream.
|
||||||
for element in saved:
|
for element in saved:
|
||||||
yield element
|
yield element
|
||||||
|
|
||||||
Note, this member of the toolkit may require significant auxiliary storage
|
This itertool may require significant auxiliary storage (depending on
|
||||||
(depending on the length of the iterable).
|
the length of the iterable).
|
||||||
|
|
||||||
|
|
||||||
.. function:: dropwhile(predicate, iterable)
|
.. function:: dropwhile(predicate, iterable)
|
||||||
|
|
||||||
Make an iterator that drops elements from the iterable as long as the predicate
|
Make an iterator that drops elements from the *iterable* while the
|
||||||
is true; afterwards, returns every element. Note, the iterator does not produce
|
*predicate* is true and afterwards returns every element. Roughly
|
||||||
*any* output until the predicate first becomes false, so it may have a lengthy
|
equivalent to::
|
||||||
start-up time. Roughly equivalent to::
|
|
||||||
|
|
||||||
def dropwhile(predicate, iterable):
|
def dropwhile(predicate, iterable):
|
||||||
# dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8
|
# dropwhile(lambda x: x<5, [1,4,6,3,8]) → 6 3 8
|
||||||
iterable = iter(iterable)
|
|
||||||
for x in iterable:
|
iterator = iter(iterable)
|
||||||
|
for x in iterator:
|
||||||
if not predicate(x):
|
if not predicate(x):
|
||||||
yield x
|
yield x
|
||||||
break
|
break
|
||||||
for x in iterable:
|
|
||||||
|
for x in iterator:
|
||||||
yield x
|
yield x
|
||||||
|
|
||||||
|
Note this does not produce *any* output until the predicate first
|
||||||
|
becomes false, so this itertool may have a lengthy start-up time.
|
||||||
|
|
||||||
|
|
||||||
.. function:: filterfalse(predicate, iterable)
|
.. function:: filterfalse(predicate, iterable)
|
||||||
|
|
||||||
Make an iterator that filters elements from iterable returning only those for
|
Make an iterator that filters elements from the *iterable* returning
|
||||||
which the predicate is false. If *predicate* is ``None``, return the items
|
only those for which the *predicate* returns a false value. If
|
||||||
that are false. Roughly equivalent to::
|
*predicate* is ``None``, returns the items that are false. Roughly
|
||||||
|
equivalent to::
|
||||||
|
|
||||||
def filterfalse(predicate, iterable):
|
def filterfalse(predicate, iterable):
|
||||||
# filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8
|
# filterfalse(lambda x: x<5, [1,4,6,3,8]) → 6 8
|
||||||
|
|
@ -473,20 +467,19 @@ loops that truncate the stream.
|
||||||
.. function:: islice(iterable, stop)
|
.. function:: islice(iterable, stop)
|
||||||
islice(iterable, start, stop[, step])
|
islice(iterable, start, stop[, step])
|
||||||
|
|
||||||
Make an iterator that returns selected elements from the iterable. If *start* is
|
Make an iterator that returns selected elements from the iterable.
|
||||||
non-zero, then elements from the iterable are skipped until start is reached.
|
Works like sequence slicing but does not support negative values for
|
||||||
Afterward, elements are returned consecutively unless *step* is set higher than
|
*start*, *stop*, or *step*.
|
||||||
one which results in items being skipped. If *stop* is ``None``, then iteration
|
|
||||||
continues until the iterator is exhausted, if at all; otherwise, it stops at the
|
|
||||||
specified position.
|
|
||||||
|
|
||||||
If *start* is ``None``, then iteration starts at zero. If *step* is ``None``,
|
If *start* is zero or ``None``, iteration starts at zero. Otherwise,
|
||||||
then the step defaults to one.
|
elements from the iterable are skipped until *start* is reached.
|
||||||
|
|
||||||
Unlike regular slicing, :func:`islice` does not support negative values for
|
If *stop* is ``None``, iteration continues until the iterator is
|
||||||
*start*, *stop*, or *step*. Can be used to extract related fields from
|
exhausted, if at all. Otherwise, it stops at the specified position.
|
||||||
data where the internal structure has been flattened (for example, a
|
|
||||||
multi-line report may list a name field on every third line).
|
If *step* is ``None``, the step defaults to one. Elements are returned
|
||||||
|
consecutively unless *step* is set higher than one which results in
|
||||||
|
items being skipped.
|
||||||
|
|
||||||
Roughly equivalent to::
|
Roughly equivalent to::
|
||||||
|
|
||||||
|
|
@ -534,18 +527,24 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: permutations(iterable, r=None)
|
.. function:: permutations(iterable, r=None)
|
||||||
|
|
||||||
Return successive *r* length permutations of elements in the *iterable*.
|
Return successive *r* length `permutations of elements
|
||||||
|
<https://www.britannica.com/science/permutation>`_ from the *iterable*.
|
||||||
|
|
||||||
If *r* is not specified or is ``None``, then *r* defaults to the length
|
If *r* is not specified or is ``None``, then *r* defaults to the length
|
||||||
of the *iterable* and all possible full-length permutations
|
of the *iterable* and all possible full-length permutations
|
||||||
are generated.
|
are generated.
|
||||||
|
|
||||||
|
The output is a subsequence of :func:`product` where entries with
|
||||||
|
repeated elements have been filtered out. The length of the output is
|
||||||
|
given by :func:`math.perm` which computes ``n! / (n - r)!`` when
|
||||||
|
``0 ≤ r ≤ n`` or zero when ``r > n``.
|
||||||
|
|
||||||
The permutation tuples are emitted in lexicographic order according to
|
The permutation tuples are emitted in lexicographic order according to
|
||||||
the order of the input *iterable*. So, if the input *iterable* is sorted,
|
the order of the input *iterable*. If the input *iterable* is sorted,
|
||||||
the output tuples will be produced in sorted order.
|
the output tuples will be produced in sorted order.
|
||||||
|
|
||||||
Elements are treated as unique based on their position, not on their
|
Elements are treated as unique based on their position, not on their
|
||||||
value. So, if the input elements are unique, there will be no repeated
|
value. If the input elements are unique, there will be no repeated
|
||||||
values within a permutation.
|
values within a permutation.
|
||||||
|
|
||||||
Roughly equivalent to::
|
Roughly equivalent to::
|
||||||
|
|
@ -578,20 +577,6 @@ loops that truncate the stream.
|
||||||
else:
|
else:
|
||||||
return
|
return
|
||||||
|
|
||||||
The code for :func:`permutations` can be also expressed as a subsequence of
|
|
||||||
:func:`product` filtered to exclude entries with repeated elements (those
|
|
||||||
from the same position in the input pool)::
|
|
||||||
|
|
||||||
def permutations(iterable, r=None):
|
|
||||||
pool = tuple(iterable)
|
|
||||||
n = len(pool)
|
|
||||||
r = n if r is None else r
|
|
||||||
for indices in product(range(n), repeat=r):
|
|
||||||
if len(set(indices)) == r:
|
|
||||||
yield tuple(pool[i] for i in indices)
|
|
||||||
|
|
||||||
The number of items returned is ``n! / (n-r)!`` when ``0 <= r <= n``
|
|
||||||
or zero when ``r > n``.
|
|
||||||
|
|
||||||
.. function:: product(*iterables, repeat=1)
|
.. function:: product(*iterables, repeat=1)
|
||||||
|
|
||||||
|
|
@ -615,10 +600,13 @@ loops that truncate the stream.
|
||||||
def product(*iterables, repeat=1):
|
def product(*iterables, repeat=1):
|
||||||
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
|
# product('ABCD', 'xy') → Ax Ay Bx By Cx Cy Dx Dy
|
||||||
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
|
# product(range(2), repeat=3) → 000 001 010 011 100 101 110 111
|
||||||
|
|
||||||
pools = [tuple(pool) for pool in iterables] * repeat
|
pools = [tuple(pool) for pool in iterables] * repeat
|
||||||
|
|
||||||
result = [[]]
|
result = [[]]
|
||||||
for pool in pools:
|
for pool in pools:
|
||||||
result = [x+[y] for x in result for y in pool]
|
result = [x+[y] for x in result for y in pool]
|
||||||
|
|
||||||
for prod in result:
|
for prod in result:
|
||||||
yield tuple(prod)
|
yield tuple(prod)
|
||||||
|
|
||||||
|
|
@ -626,6 +614,7 @@ loops that truncate the stream.
|
||||||
keeping pools of values in memory to generate the products. Accordingly,
|
keeping pools of values in memory to generate the products. Accordingly,
|
||||||
it is only useful with finite inputs.
|
it is only useful with finite inputs.
|
||||||
|
|
||||||
|
|
||||||
.. function:: repeat(object[, times])
|
.. function:: repeat(object[, times])
|
||||||
|
|
||||||
Make an iterator that returns *object* over and over again. Runs indefinitely
|
Make an iterator that returns *object* over and over again. Runs indefinitely
|
||||||
|
|
@ -650,12 +639,12 @@ loops that truncate the stream.
|
||||||
>>> list(map(pow, range(10), repeat(2)))
|
>>> list(map(pow, range(10), repeat(2)))
|
||||||
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
[0, 1, 4, 9, 16, 25, 36, 49, 64, 81]
|
||||||
|
|
||||||
|
|
||||||
.. function:: starmap(function, iterable)
|
.. function:: starmap(function, iterable)
|
||||||
|
|
||||||
Make an iterator that computes the function using arguments obtained from
|
Make an iterator that computes the *function* using arguments obtained
|
||||||
the iterable. Used instead of :func:`map` when argument parameters are already
|
from the *iterable*. Used instead of :func:`map` when argument
|
||||||
grouped in tuples from a single iterable (when the data has been
|
parameters have already been "pre-zipped" into tuples.
|
||||||
"pre-zipped").
|
|
||||||
|
|
||||||
The difference between :func:`map` and :func:`starmap` parallels the
|
The difference between :func:`map` and :func:`starmap` parallels the
|
||||||
distinction between ``function(a,b)`` and ``function(*c)``. Roughly
|
distinction between ``function(a,b)`` and ``function(*c)``. Roughly
|
||||||
|
|
@ -669,8 +658,8 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: takewhile(predicate, iterable)
|
.. function:: takewhile(predicate, iterable)
|
||||||
|
|
||||||
Make an iterator that returns elements from the iterable as long as the
|
Make an iterator that returns elements from the *iterable* as long as
|
||||||
predicate is true. Roughly equivalent to::
|
the *predicate* is true. Roughly equivalent to::
|
||||||
|
|
||||||
def takewhile(predicate, iterable):
|
def takewhile(predicate, iterable):
|
||||||
# takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4
|
# takewhile(lambda x: x<5, [1,4,6,3,8]) → 1 4
|
||||||
|
|
@ -726,9 +715,15 @@ loops that truncate the stream.
|
||||||
|
|
||||||
.. function:: zip_longest(*iterables, fillvalue=None)
|
.. function:: zip_longest(*iterables, fillvalue=None)
|
||||||
|
|
||||||
Make an iterator that aggregates elements from each of the iterables. If the
|
Make an iterator that aggregates elements from each of the
|
||||||
iterables are of uneven length, missing values are filled-in with *fillvalue*.
|
*iterables*.
|
||||||
Iteration continues until the longest iterable is exhausted. Roughly equivalent to::
|
|
||||||
|
If the iterables are of uneven length, missing values are filled-in
|
||||||
|
with *fillvalue*. If not specified, *fillvalue* defaults to ``None``.
|
||||||
|
|
||||||
|
Iteration continues until the longest iterable is exhausted.
|
||||||
|
|
||||||
|
Roughly equivalent to::
|
||||||
|
|
||||||
def zip_longest(*iterables, fillvalue=None):
|
def zip_longest(*iterables, fillvalue=None):
|
||||||
# zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-
|
# zip_longest('ABCD', 'xy', fillvalue='-') → Ax By C- D-
|
||||||
|
|
@ -754,8 +749,7 @@ loops that truncate the stream.
|
||||||
|
|
||||||
If one of the iterables is potentially infinite, then the :func:`zip_longest`
|
If one of the iterables is potentially infinite, then the :func:`zip_longest`
|
||||||
function should be wrapped with something that limits the number of calls
|
function should be wrapped with something that limits the number of calls
|
||||||
(for example :func:`islice` or :func:`takewhile`). If not specified,
|
(for example :func:`islice` or :func:`takewhile`).
|
||||||
*fillvalue* defaults to ``None``.
|
|
||||||
|
|
||||||
|
|
||||||
.. _itertools-recipes:
|
.. _itertools-recipes:
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue