Raymond Hettinger <rhettin...@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 <rep...@bugs.python.org>
<http://bugs.python.org/issue5669>
_______________________________________
_______________________________________________
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com

Reply via email to