From: Raymond Hettinger Date: Thu, 13 Mar 2008 19:33:34 +0000 (+0000) Subject: Simplify the nlargest() code using heappushpop(). X-Git-Tag: v2.6a2~336 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=83aa6a3b1a6a406b3cde2fa7daa5d5b1db0cc6a7;p=thirdparty%2FPython%2Fcpython.git Simplify the nlargest() code using heappushpop(). --- diff --git a/Lib/heapq.py b/Lib/heapq.py index 23f5fcbfda07..4af9af17717c 100644 --- a/Lib/heapq.py +++ b/Lib/heapq.py @@ -193,13 +193,9 @@ def nlargest(n, iterable): if not result: return result heapify(result) - _heapreplace = heapreplace - sol = result[0] # sol --> smallest of the nlargest + _heappushpop = heappushpop for elem in it: - if elem <= sol: - continue - _heapreplace(result, elem) - sol = result[0] + heappushpop(result, elem) result.sort(reverse=True) return result