diff options
Diffstat (limited to 'Doc/library/itertools.rst')
-rw-r--r-- | Doc/library/itertools.rst | 27 |
1 files changed, 13 insertions, 14 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index d04f33b..826fa45 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -95,21 +95,20 @@ loops that truncate the stream. def combinations(iterable, r): pool = tuple(iterable) - if pool: - n = len(pool) - vec = range(r) - yield tuple(pool[i] for i in vec) - while 1: - for i in reversed(range(r)): - if vec[i] == i + n-r: - continue - vec[i] += 1 - for j in range(i+1, r): - vec[j] = vec[j-1] + 1 - yield tuple(pool[i] for i in vec) + n = len(pool) + assert 0 <= r <= n + vec = range(r) + yield tuple(pool[i] for i in vec) + while 1: + for i in reversed(range(r)): + if vec[i] != i + n - r: break - else: - return + else: + return + vec[i] += 1 + for j in range(i+1, r): + vec[j] = vec[j-1] + 1 + yield tuple(pool[i] for i in vec) .. versionadded:: 2.6 |