Keeping track of the N largest values

Stefan Sonnenberg-Carstens stefan.sonnenberg at pythonmeister.com
Mon Dec 27 14:27:19 EST 2010


Am 26.12.2010 19:51, schrieb Stefan Sonnenberg-Carstens:
> l = []
> K = 10
>
> while 1:
>     a = input()
>     if len(l) == K:
>         l.remove(min(l))
>     l=[x for x in l if x < a] + [a] + [x for x in l if x > a]
>     print l 
A minor fault made it into my prog:

l = [0]
K = 10

while 1:
     a = input()
     l=[x for x in l if x < a] + [a] + [x for x in l if x > a]
     if len(l) == K:
         l.remove(min(l))
     print l



More information about the Python-list mailing list