Conversation
The docs and both docstrings say nlargest and nsmallest are equivalent to sorted(iterable, key=key)[:n] and sorted(iterable, key=key, reverse=True)[:n], but that is wrong when n is negative. nsmallest(-1, [5, 3, 8, 1, 9, 2]) returns [] while sorted(...)[:-1] returns [1, 2, 3, 5, 8]. nsmallest builds its first chunk with zip(range(n), it) and nlargest with zip(range(0, -n, -1), it). Both are empty when n is negative, so both return [] at the `if not result` check. n == 0 agrees, so it is only negative n that differs. Changed the slice to [:max(n, 0)] rather than adding a caveat, so the equivalence stays an expression you can actually run. Applies to Doc/library/heapq.rst and to the two docstrings in Lib/heapq.py. test_nsmallest and test_nlargest already check this equivalence, but only with non-negative n, so negative values are added there and the expected expression is corrected to match.
|
Most changes to Python require a NEWS entry. Add one using the blurb_it web app or the blurb command-line tool. If this change has little impact on Python users, wait for a maintainer to apply the |
Documentation build overview
|
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
The docs and the docstrings for
nlargestandnsmallestboth say:That is not true for a negative
n:So
nlargestandnsmallestare not equivalent to that slice when n is a negative number.nsmallestbuilds its first chunk with[(elem, i) for i, elem in zip(range(n), it)].range(-1)is empty, and thats whyresultends up empty and the function returns[]right there at theif not resultcheck.nlargestends up the same way, it just useszip(range(0, -n, -1), it)instead, which is also empty when n is negative.n == 0is fine, both give[], so its only the negative n that differs.Rather than adding a caveat i changed the slice to
[:max(n, 0)], which keeps it an expression you can paste into a REPL. I checked that against both functions over every list up to length 5 with lots of duplicates, and over random inputs with akey=and with generators, and it matches everywhere.test_nsmallestandtest_nlargestinLib/test/test_heapq.pyalready check this equivalence, but the n values they loop over are(0, 1, 2, 10, 100, 400, 999, 1000, 1100), so a negative n never gets tried. i added some negative values there and corrected the expected expression to match.The same wording is in
Doc/library/heapq.rstand in the two docstrings inLib/heapq.py, so i changed all four. but there is no behaviour change.