Keeping track of the N largest values

Paul Rubin no.email at nospam.invalid
Sat Dec 25 14:41:58 EST 2010


Roy Smith <roy at panix.com> writes:
>>   from heapq import nlargest
>>   top = nlargest(K, input())

> In addition to finding the K largest values, I *also* need to do some
> other processing on all the values ....  The problem with nlargest()
> is that it doesn't give me a hook to do that.

    def processed_input():
      for x in input():
        process(x)
        yield x

    top = nlargest(K, processed_input())

You could also write that more consisely with genexps but it's a bit
clumsy.



More information about the Python-list mailing list