Re: 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?

2010-04-23 Thread Bryan
Steven D'Aprano wrote: John Nagle wrote:    Is nlargest smart enough to decide when it's cheaper to track the N largest entries on a linear pass through the list than to sort? It *always* does a linear pass through the list (linear, that is in the length of the entire list). It tracks the n

Re: 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?

2010-04-23 Thread Grant Edwards
On 2010-04-22, D'Arcy J.M. Cain da...@druid.net wrote: On Thu, 22 Apr 2010 15:04:01 +0100 Tim Golden m...@timgolden.me.uk wrote: So please tell me if there is one or not. I really need this soon. Appreciate a lot. Assuming top-k doesn't mean something obscurely statistical: You really

Re: 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?

2010-04-23 Thread Steven Howe
Really! Learn to use google better. I just used python sort list Look at: http://wiki.python.org/moin/HowTo/Sorting Read about list.sort. Try, at a command prompt (assuming you have a unix shell), pydoc list search for sort; read it. It mentions 'reverse'. then slice the list to your desired

Re: 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?

2010-04-23 Thread Raymond Hettinger
On Apr 22, 10:49 am, John Nagle na...@animats.com wrote: Chris Rebert wrote: 2010/4/22 Jo Chan csj...@gmail.com: 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? I only know about max which is poorly a top-1

Re: 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?

2010-04-23 Thread Raymond Hettinger
On Apr 23, 1:24 am, Bryan bryanjugglercryptograp...@yahoo.com wrote: That is interesting. The above algorithm for nlargest is better, but to use it for nsmallest requires a largest-on-top heap, which the module does not bother to implement. FWIW, the pure python versions differ because they

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?

2010-04-22 Thread Jo Chan
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? I only know about max which is poorly a top-1 maximum function, now I want more yet I am lazy enough that don't want to write one by myself. So please tell me if there is

Re: 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?

2010-04-22 Thread Tim Golden
On 22/04/2010 14:57, Jo Chan wrote: 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? I only know about max which is poorly a top-1 maximum function, now I want more yet I am lazy enough that don't want to write one

Re: 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?

2010-04-22 Thread Chris Rebert
2010/4/22 Jo Chan csj...@gmail.com: 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? I only know about max which is poorly a top-1 maximum function, now I want more yet I am lazy enough that don't want to write one by

Re: 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?

2010-04-22 Thread Xavier Ho
On Fri, Apr 23, 2010 at 12:04 AM, Tim Golden m...@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]

Re: 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?

2010-04-22 Thread Jo Chan
Cool! Thanks a lot! That's exactly what I want. Best regards, Songjian On Thu, Apr 22, 2010 at 10:04 PM, Chris Rebert creb...@ucsd.edu wrote: 2010/4/22 Jo Chan csj...@gmail.com: Hi,friends. I wanna ask if there is a function which is able to take a list as argument and then return its

Re: 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?

2010-04-22 Thread Jo Chan
Yeah... but actually I need something more efficient, like heap. Thank you for your help though. Best regards, Songjian On Thu, Apr 22, 2010 at 10:04 PM, Tim Golden m...@timgolden.me.uk wrote: On 22/04/2010 14:57, Jo Chan wrote: Hi,friends. I wanna ask if there is a function which is

Re: 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?

2010-04-22 Thread D'Arcy J.M. Cain
On Fri, 23 Apr 2010 00:07:18 +1000 Xavier Ho cont...@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. -- D'Arcy

Re: 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?

2010-04-22 Thread Xavier Ho
On Fri, Apr 23, 2010 at 12:23 AM, D'Arcy J.M. Cain da...@druid.net wrote: Now try returning the top two or four numbers. numbers = [1, 4, 5, 3, 7, 8] sorted(numbers)[-2:] [7, 8] sorted(numbers)[-4:] [4, 5, 7, 8] I see what you mean. This is not as intuitive, is it? Cheers, Xav --

Re: 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?

2010-04-22 Thread D'Arcy J.M. Cain
On Thu, 22 Apr 2010 15:04:01 +0100 Tim Golden m...@timgolden.me.uk wrote: So please tell me if there is one or not. I really need this soon. Appreciate a lot. Assuming top-k doesn't mean something obscurely statistical: You really shouldn't do people's homework for them. It doesn't do

Re: 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?

2010-04-22 Thread John Nagle
Chris Rebert wrote: 2010/4/22 Jo Chan csj...@gmail.com: 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? I only know about max which is poorly a top-1 maximum function, now I want more yet I am lazy enough that don't

Re: 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?

2010-04-22 Thread Rhodri James
On Thu, 22 Apr 2010 15:23:29 +0100, D'Arcy J.M. Cain da...@druid.net wrote: On Fri, 23 Apr 2010 00:07:18 +1000 Xavier Ho cont...@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]

Re: 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?

2010-04-22 Thread Steven D'Aprano
On Thu, 22 Apr 2010 10:49:29 -0700, John Nagle wrote: Is nlargest smart enough to decide when it's cheaper to track the N largest entries on a linear pass through the list than to sort? Doesn't appear to do so. From Python 3.1: def nlargest(n, iterable): Find the n largest elements in