summaryrefslogtreecommitdiffstats
path: root/Lib/heapq.py
diff options
context:
space:
mode:
authorRaymond Hettinger <python@rcn.com>2014-05-19 21:13:45 (GMT)
committerRaymond Hettinger <python@rcn.com>2014-05-19 21:13:45 (GMT)
commit356902dd750d8d42bf2fed4cb49811cc85e23701 (patch)
treeaaa69fc69a32f839a36750a182f3a3d86cbe4e7d /Lib/heapq.py
parent1f54814094afb87a419b7e6e4e02a498f4cf23c4 (diff)
downloadcpython-356902dd750d8d42bf2fed4cb49811cc85e23701.zip
cpython-356902dd750d8d42bf2fed4cb49811cc85e23701.tar.gz
cpython-356902dd750d8d42bf2fed4cb49811cc85e23701.tar.bz2
Small code and comment cleanups
Diffstat (limited to 'Lib/heapq.py')
-rw-r--r--Lib/heapq.py18
1 files changed, 4 insertions, 14 deletions
diff --git a/Lib/heapq.py b/Lib/heapq.py
index 14a7a86..41626c5 100644
--- a/Lib/heapq.py
+++ b/Lib/heapq.py
@@ -141,9 +141,8 @@ def heappop(heap):
returnitem = heap[0]
heap[0] = lastelt
_siftup(heap, 0)
- else:
- returnitem = lastelt
- return returnitem
+ return returnitem
+ return lastelt
def heapreplace(heap, item):
"""Pop and return the current smallest value, and add the new item.
@@ -357,14 +356,14 @@ def merge(*iterables):
# Algorithm notes for nlargest() and nsmallest()
# ==============================================
#
-# Makes just a single pass over the data while keeping the k most extreme values
+# Make a single pass over the data while keeping the k most extreme values
# in a heap. Memory consumption is limited to keeping k values in a list.
#
# Measured performance for random inputs:
#
# number of comparisons
# n inputs k-extreme values (average of 5 trials) % more than min()
-# ------------- ---------------- - ------------------- -----------------
+# ------------- ---------------- --------------------- -----------------
# 1,000 100 3,317 133.2%
# 10,000 100 14,046 40.5%
# 100,000 100 105,749 5.7%
@@ -542,15 +541,6 @@ def nlargest(n, iterable, key=None):
if __name__ == "__main__":
- # Simple sanity test
- heap = []
- data = [1, 3, 5, 7, 9, 2, 4, 6, 8, 0]
- for item in data:
- heappush(heap, item)
- sort = []
- while heap:
- sort.append(heappop(heap))
- print(sort)
import doctest
doctest.testmod()