[issue5669] Extra heapq nlargest/nsmallest option for including ties
Raymond Hettinger
report at bugs.python.org
Thu Apr 2 21:41:01 CEST 2009
Raymond Hettinger <rhettinger at users.sourceforge.net> added the comment:
FWIW, an easy way to pare down millions of entries to dozens is to use a
bigger heap:
from heapq import nsmallest
def nsmallest_with_ties(n, iterable, scale=2):
ext = n * scale
s = nsmallest(ext, iterable)
lastplace = s[n-1]
if s[-1] == lastplace:
raise ValueError('may not have found all ties')
for i in range(n, ext):
if s[i] != lastplace:
break
return s[:i]
n = 3
s = [4,3,5,7,4,7,4,3]
print nsmallest_with_ties(3, s)
----------
_______________________________________
Python tracker <report at bugs.python.org>
<http://bugs.python.org/issue5669>
_______________________________________
More information about the Python-bugs-list
mailing list