diff options
author | Raymond Hettinger <python@rcn.com> | 2009-01-08 05:20:19 (GMT) |
---|---|---|
committer | Raymond Hettinger <python@rcn.com> | 2009-01-08 05:20:19 (GMT) |
commit | 825758c50b3c8006db16c1b8627e417db32a1d23 (patch) | |
tree | 119e0903dc54f33806e5eae267ee52328b6d027c /Doc | |
parent | cd610ae7f21db5782b76d562002b27959d7e87b7 (diff) | |
download | cpython-825758c50b3c8006db16c1b8627e417db32a1d23.zip cpython-825758c50b3c8006db16c1b8627e417db32a1d23.tar.gz cpython-825758c50b3c8006db16c1b8627e417db32a1d23.tar.bz2 |
- Issue 4816: itertools.combinations() and itertools.product were raising
a ValueError for values of *r* larger than the input iterable. They now
correctly return an empty iterator.
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 13 |
1 files changed, 12 insertions, 1 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 67646c6..aef3f6a 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -108,6 +108,8 @@ loops that truncate the stream. # combinations(range(4), 3) --> 012 013 023 123 pool = tuple(iterable) n = len(pool) + if r > n: + return indices = range(r) yield tuple(pool[i] for i in indices) while 1: @@ -132,6 +134,9 @@ 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``. + .. versionadded:: 2.6 .. function:: count([n]) @@ -399,6 +404,8 @@ loops that truncate the stream. pool = tuple(iterable) n = len(pool) r = n if r is None else r + if r > n: + return indices = range(n) cycles = range(n, n-r, -1) yield tuple(pool[i] for i in indices[:r]) @@ -428,6 +435,9 @@ 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``. + .. versionadded:: 2.6 .. function:: product(*iterables[, repeat]) @@ -674,7 +684,8 @@ which incur interpreter overhead. 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" + "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 |