diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-08 21:01:54 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-08 21:01:54 (GMT) |
commit | 5bad41eefc9f80298bb1abe00a5475be8b015c57 (patch) | |
tree | d4b8064de3b2baaa474fb80133a1b6970ad69c07 /Doc | |
parent | 5e4e4278c9ddec1d143962a53de653442a61f443 (diff) | |
download | cpython-5bad41eefc9f80298bb1abe00a5475be8b015c57.zip cpython-5bad41eefc9f80298bb1abe00a5475be8b015c57.tar.gz cpython-5bad41eefc9f80298bb1abe00a5475be8b015c57.tar.bz2 |
Merge in r68394 fixing itertools.permutations() and combinations().
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 15 |
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 |