Keeping track of the N largest values

Miki miki.tebeka at gmail.com
Tue Dec 28 16:59:36 EST 2010


> I'm processing a stream of N numbers and want to keep track of the K 
> largest.  There's too many numbers in the stream (i.e. N is too large) 
> to keep in memory at once.  K is small (100 would be typical).
> ...

deque can be bounded by maxsize, might fit the bill:
    >>> from collections import deque
    >>> d = deque([], 3)
    >>> for i in range(10): d.append(i)
    >>> d
        deque([7, 8, 9], maxlen=3)
    >>> 


HTH,
--
Miki



More information about the Python-list mailing list