Message322307
I would like to make three small improvements to the "heapq" module.
1) The "nsmallest" function has the following code (a similar code exists in the "nlargest" function):
# When n>=size, it's faster to use sorted()
try:
size = len(iterable)
except (TypeError, AttributeError):
pass
else:
if n >= size:
return sorted(iterable, key=key)[:n]
I think "[:n]" is redundant, because "iterable" contains no more than n elements.
Therefore, that code can be rewritten as follows:
# When n>=size, it's faster to use sorted()
try:
size = len(iterable)
except (TypeError, AttributeError):
pass
else:
if n >= size:
return sorted(iterable, key=key)
2) It seems to me that the line:
for i in reversed(range(n//2)):
will be more optimal in this version:
for i in range(n//2 + 1, -1, -1):
3) Top-level functions can be surrounded with two blank lines for greater compliance with PEP-8. |
|
Date |
User |
Action |
Args |
2018-07-24 15:06:20 | amper | set | recipients:
+ amper |
2018-07-24 15:06:20 | amper | set | messageid: <1532444780.82.0.56676864532.issue34210@psf.upfronthosting.co.za> |
2018-07-24 15:06:20 | amper | link | issue34210 messages |
2018-07-24 15:06:20 | amper | create | |
|