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?
Raymond Hettinger
python at rcn.com
Fri Apr 23 20:34:37 EDT 2010
On Apr 22, 10:49 am, John Nagle <na... at animats.com> wrote:
> Chris Rebert wrote:
> > 2010/4/22 Jo Chan <csj... at 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 myself.
>
> >http://docs.python.org/library/heapq.html#heapq.nlargest
>
> > Cheers,
> > Chris
> > --
> >http://blog.rebertia.com
>
> 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?
nlargest() has gotten smarter over time. So, the answer depends on
which version you're using.
Here's a snippet from http://svn.python.org/view/python/branches/py3k/Lib/heapq.py?revision=70695&view=markup
def nlargest(n, iterable, key=None):
"""Find the n largest elements in a dataset.
Equivalent to: sorted(iterable, key=key, reverse=True)[:n]
"""
# Short-cut for n==1 is to use max() when len(iterable)>0
if n == 1:
it = iter(iterable)
head = list(islice(it, 1))
if not head:
return []
if key is None:
return [max(chain(head, it))]
return [max(chain(head, it), key=key)]
# When n>=size, it's faster to use sort()
try:
size = len(iterable)
except (TypeError, AttributeError):
pass
else:
if n >= size:
return sorted(iterable, key=key, reverse=True)[:n]
# When key is none, use simpler decoration
if key is None:
it = zip(iterable, count(0,-1)) # decorate
result = _nlargest(n, it)
return [r[0] for r in result] #
undecorate
# General case, slowest method
in1, in2 = tee(iterable)
it = zip(map(key, in1), count(0,-1), in2) # decorate
result = _nlargest(n, it)
return [r[2] for r in result] #
undecorate
More information about the Python-list
mailing list