Keeping track of the N largest values

Roy Smith roy at panix.com
Sun Dec 26 10:12:33 EST 2010


In article 
<e05e480b-8956-4984-b4cc-9a1666380eed at l32g2000yqc.googlegroups.com>,
 n00m <n00m at narod.ru> wrote:

> from bisect import insort_left
> 
> K = 5
> top = []
> while 1:
>     x = input()
>     if len(top) < K:
>         insort_left(top, x)
>     elif x > top[0]:
>         del top[0]
>         insort_left(top, x)
>     print top
> 
> 
> will be enough

Hmmm, that's an interesting idea.  Thanks.



More information about the Python-list mailing list