summaryrefslogtreecommitdiffstats
path: root/Doc
diff options
context:
space:
mode:
Diffstat (limited to 'Doc')
-rw-r--r--Doc/library/itertools.rst15
1 files changed, 12 insertions, 3 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst
index b384cc8..db10b6d 100644
--- a/Doc/library/itertools.rst
+++ b/Doc/library/itertools.rst
@@ -104,7 +104,9 @@ loops that truncate the stream.
# combinations(range(4), 3) --> 012 013 023 123
pool = tuple(iterable)
n = len(pool)
- indices = range(r)
+ if r > n:
+ return
+ indices = list(range(r))
yield tuple(pool[i] for i in indices)
while 1:
for i in reversed(range(r)):
@@ -128,6 +130,8 @@ loops that truncate the stream.
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:: count([n])
@@ -325,7 +329,9 @@ loops that truncate the stream.
pool = tuple(iterable)
n = len(pool)
r = n if r is None else r
- indices = range(n)
+ if r > n:
+ return
+ indices = list(range(n))
cycles = range(n, n-r, -1)
yield tuple(pool[i] for i in indices[:r])
while n:
@@ -354,6 +360,8 @@ loops that truncate the stream.
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])
@@ -593,7 +601,8 @@ which incur interpreter overhead.
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"
+ "combinations_with_replacement('ABC', 2) --> AA AB AC BB BC CC"
+ # number items returned: (n+r-1)! / r! / (n-1)!
pool = tuple(iterable)
n = len(pool)
indices = [0] * r