Hi, friends. I wanna ask if there is a function which is able to take a list as argument and then return its top-k maximums?

Xavier Ho contact at xavierho.com
Thu Apr 22 10:07:18 EDT 2010


On Fri, Apr 23, 2010 at 12:04 AM, Tim Golden <mail at timgolden.me.uk> wrote:

> Assuming top-k doesn't mean something obscurely statistical:
>
> l = [1,2, 3, 4, 5]
> k = 3
> print (sorted (l, reverse=True)[:k])
>

You don't really need to reverse sort there:

>>> numbers = [1, 4, 5, 3, 7, 8]
>>> sorted(numbers)[3:]
[5, 7, 8]

Cheers,
Xav
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20100423/b02beeac/attachment.html>


More information about the Python-list mailing list