Commit 1bfbe78b authored by Raymond Hettinger's avatar Raymond Hettinger Committed by GitHub

Improve clarity (and small speed-up) by using tuple unpacking (#3289)

parent 5ce10633
...@@ -498,10 +498,10 @@ def nsmallest(n, iterable, key=None): ...@@ -498,10 +498,10 @@ def nsmallest(n, iterable, key=None):
for elem in it: for elem in it:
if elem < top: if elem < top:
_heapreplace(result, (elem, order)) _heapreplace(result, (elem, order))
top = result[0][0] top, _order = result[0]
order += 1 order += 1
result.sort() result.sort()
return [r[0] for r in result] return [elem for (elem, order) in result]
# General case, slowest method # General case, slowest method
it = iter(iterable) it = iter(iterable)
...@@ -516,10 +516,10 @@ def nsmallest(n, iterable, key=None): ...@@ -516,10 +516,10 @@ def nsmallest(n, iterable, key=None):
k = key(elem) k = key(elem)
if k < top: if k < top:
_heapreplace(result, (k, order, elem)) _heapreplace(result, (k, order, elem))
top = result[0][0] top, _order, _elem = result[0]
order += 1 order += 1
result.sort() result.sort()
return [r[2] for r in result] return [elem for (k, order, elem) in result]
def nlargest(n, iterable, key=None): def nlargest(n, iterable, key=None):
"""Find the n largest elements in a dataset. """Find the n largest elements in a dataset.
...@@ -559,10 +559,10 @@ def nlargest(n, iterable, key=None): ...@@ -559,10 +559,10 @@ def nlargest(n, iterable, key=None):
for elem in it: for elem in it:
if top < elem: if top < elem:
_heapreplace(result, (elem, order)) _heapreplace(result, (elem, order))
top = result[0][0] top, _order = result[0]
order -= 1 order -= 1
result.sort(reverse=True) result.sort(reverse=True)
return [r[0] for r in result] return [elem for (elem, order) in result]
# General case, slowest method # General case, slowest method
it = iter(iterable) it = iter(iterable)
...@@ -577,10 +577,10 @@ def nlargest(n, iterable, key=None): ...@@ -577,10 +577,10 @@ def nlargest(n, iterable, key=None):
k = key(elem) k = key(elem)
if top < k: if top < k:
_heapreplace(result, (k, order, elem)) _heapreplace(result, (k, order, elem))
top = result[0][0] top, _order, _elem = result[0]
order -= 1 order -= 1
result.sort(reverse=True) result.sort(reverse=True)
return [r[2] for r in result] return [elem for (k, order, elem) in result]
# If available, use C implementation # If available, use C implementation
try: try:
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment