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?
rhodri at wildebst.demon.co.uk
Thu Apr 22 18:50:55 EDT 2010
On Thu, 22 Apr 2010 15:23:29 +0100, D'Arcy J.M. Cain <darcy at druid.net>
> On Fri, 23 Apr 2010 00:07:18 +1000
> Xavier Ho <contact at xavierho.com> wrote:
>> > print (sorted (l, reverse=True)[:k])
>> You don't really need to reverse sort there:
> True but...
>> >>> numbers = [1, 4, 5, 3, 7, 8]
>> >>> sorted(numbers)[3:]
>> [5, 7, 8]
> Now try returning the top two or four numbers.
[4, 5, 7, 8]
or in general
That said, reverse sorting is probably clearer, and might be marginally
faster, though frankly I can't be bothered to check that.
Rhodri James *-* Wildebeeste Herder to the Masses
More information about the Python-list