[Numpy-discussion] k maximal elements

Olivier Delalleau shish at keba.be
Mon Jun 6 08:52:18 EDT 2011


I don't really understand your proposed solution, but you can do something
like:

import heapq
q = list(x)
heapq.heapify(q)
k_smallest = [heapq.heappop(q) for i in xrange(k)]

which is in O(n + k log n)

-=- Olivier

2011/6/6 Alex Ter-Sarkissov <ater1980 at gmail.com>

> I have a vector of positive integers length n. Is there a simple (i.e.
> without sorting/ranking) of 'pulling out' k larrgest (or smallest) values.
> Something like
>
> *sum(x[sum(x,1)>(max(sum(x,1)+min(sum(x,1))))/2,])*
>
> but smarter
>
> _______________________________________________
> NumPy-Discussion mailing list
> NumPy-Discussion at scipy.org
> http://mail.scipy.org/mailman/listinfo/numpy-discussion
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/numpy-discussion/attachments/20110606/233926ae/attachment.html>


More information about the NumPy-Discussion mailing list