summaryrefslogtreecommitdiffstats
path: root/Lib/heapq.py
diff options
context:
space:
mode:
authorRaymond Hettinger <rhettinger@users.noreply.github.com>2017-09-04 18:47:58 (GMT)
committerGitHub <noreply@github.com>2017-09-04 18:47:58 (GMT)
commit1bfbe78b03e6ee3ee7de938b5e39015dd08b0302 (patch)
tree4532fae365e7df5daf42b59746bcdc79656901a2 /Lib/heapq.py
parent5ce1063345419407871c51c604d2553777bc6cca (diff)
downloadcpython-1bfbe78b03e6ee3ee7de938b5e39015dd08b0302.zip
cpython-1bfbe78b03e6ee3ee7de938b5e39015dd08b0302.tar.gz
cpython-1bfbe78b03e6ee3ee7de938b5e39015dd08b0302.tar.bz2
Improve clarity (and small speed-up) by using tuple unpacking (#3289)
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r--Lib/heapq.py16
1 files changed, 8 insertions, 8 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index 0b3e89a..b31f418 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -498,10 +498,10 @@ def nsmallest(n, iterable, key=None):
for elem in it:
if elem < top:
_heapreplace(result, (elem, order))
- top = result[0][0]
+ top, _order = result[0]
order += 1
result.sort()
- return [r[0] for r in result]
+ return [elem for (elem, order) in result]
# General case, slowest method
it = iter(iterable)
@@ -516,10 +516,10 @@ def nsmallest(n, iterable, key=None):
k = key(elem)
if k < top:
_heapreplace(result, (k, order, elem))
- top = result[0][0]
+ top, _order, _elem = result[0]
order += 1
result.sort()
- return [r[2] for r in result]
+ return [elem for (k, order, elem) in result]
def nlargest(n, iterable, key=None):
"""Find the n largest elements in a dataset.
@@ -559,10 +559,10 @@ def nlargest(n, iterable, key=None):
for elem in it:
if top < elem:
_heapreplace(result, (elem, order))
- top = result[0][0]
+ top, _order = result[0]
order -= 1
result.sort(reverse=True)
- return [r[0] for r in result]
+ return [elem for (elem, order) in result]
# General case, slowest method
it = iter(iterable)
@@ -577,10 +577,10 @@ def nlargest(n, iterable, key=None):
k = key(elem)
if top < k:
_heapreplace(result, (k, order, elem))
- top = result[0][0]
+ top, _order, _elem = result[0]
order -= 1
result.sort(reverse=True)
- return [r[2] for r in result]
+ return [elem for (k, order, elem) in result]
# If available, use C implementation
try: