diff options
author | Miss Islington (bot) <31488909+miss-islington@users.noreply.github.com> | 2024-03-13 07:19:12 (GMT) |
---|---|---|
committer | GitHub <noreply@github.com> | 2024-03-13 07:19:12 (GMT) |
commit | e5c1456bbd11b7ceb4a26e23cf1b55b0631ca149 (patch) | |
tree | 81a5cd936a2f2298c66b56ff00b68b0852b70c47 /Doc | |
parent | 96c76047096550f193a83d0870b94d378935783f (diff) | |
download | cpython-e5c1456bbd11b7ceb4a26e23cf1b55b0631ca149.zip cpython-e5c1456bbd11b7ceb4a26e23cf1b55b0631ca149.tar.gz cpython-e5c1456bbd11b7ceb4a26e23cf1b55b0631ca149.tar.bz2 |
[3.12] Modernize roundrobin() recipe and improve variable names (gh-116710) (gh-116711)
Diffstat (limited to 'Doc')
-rw-r--r-- | Doc/library/itertools.rst | 31 |
1 files changed, 14 insertions, 17 deletions
diff --git a/Doc/library/itertools.rst b/Doc/library/itertools.rst index 53a8246..df8cde4 100644 --- a/Doc/library/itertools.rst +++ b/Doc/library/itertools.rst @@ -924,31 +924,25 @@ which incur interpreter overhead. # grouper('ABCDEFG', 3, fillvalue='x') --> ABC DEF Gxx # grouper('ABCDEFG', 3, incomplete='strict') --> ABC DEF ValueError # grouper('ABCDEFG', 3, incomplete='ignore') --> ABC DEF - args = [iter(iterable)] * n + iterators = [iter(iterable)] * n match incomplete: case 'fill': - return zip_longest(*args, fillvalue=fillvalue) + return zip_longest(*iterators, fillvalue=fillvalue) case 'strict': - return zip(*args, strict=True) + return zip(*iterators, strict=True) case 'ignore': - return zip(*args) + return zip(*iterators) case _: raise ValueError('Expected fill, strict, or ignore') def roundrobin(*iterables): "Visit input iterables in a cycle until each is exhausted." # roundrobin('ABC', 'D', 'EF') --> A D E B F C - # Recipe credited to George Sakkis - num_active = len(iterables) - nexts = cycle(iter(it).__next__ for it in iterables) - while num_active: - try: - for next in nexts: - yield next() - except StopIteration: - # Remove the iterator we just exhausted from the cycle. - num_active -= 1 - nexts = cycle(islice(nexts, num_active)) + # Algorithm credited to George Sakkis + iterators = map(iter, iterables) + for num_active in range(len(iterables), 0, -1): + iterators = cycle(islice(iterators, num_active)) + yield from map(next, iterators) def partition(predicate, iterable): """Partition entries into false entries and true entries. @@ -989,10 +983,10 @@ The following recipes have a more mathematical flavor: s = list(iterable) return chain.from_iterable(combinations(s, r) for r in range(len(s)+1)) - def sum_of_squares(it): + def sum_of_squares(iterable): "Add up the squares of the input values." # sum_of_squares([10, 20, 30]) --> 1400 - return math.sumprod(*tee(it)) + return math.sumprod(*tee(iterable)) def reshape(matrix, cols): "Reshape a 2-D matrix to have a given number of columns." @@ -1558,6 +1552,9 @@ The following recipes have a more mathematical flavor: >>> list(roundrobin('abc', 'd', 'ef')) ['a', 'd', 'e', 'b', 'f', 'c'] + >>> ranges = [range(5, 1000), range(4, 3000), range(0), range(3, 2000), range(2, 5000), range(1, 3500)] + >>> collections.Counter(roundrobin(ranges)) == collections.Counter(ranges) + True >>> def is_odd(x): ... return x % 2 == 1 |