I don't really understand your proposed solution, but you can do something like:<br><br>import heapq<br>q = list(x)<br>heapq.heapify(q)<br>k_smallest = [heapq.heappop(q) for i in xrange(k)]<br><br>which is in O(n + k log n)<br>
<br>-=- Olivier<br><br><div class="gmail_quote">2011/6/6 Alex Ter-Sarkissov <span dir="ltr"><<a href="mailto:ater1980@gmail.com">ater1980@gmail.com</a>></span><br><blockquote class="gmail_quote" style="margin: 0pt 0pt 0pt 0.8ex; border-left: 1px solid rgb(204, 204, 204); padding-left: 1ex;">
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 <br><br><i>sum(x[sum(x,1)>(max(sum(x,1)+min(sum(x,1))))/2,])</i><br>
<br>but smarter <br>
<br>_______________________________________________<br>
NumPy-Discussion mailing list<br>
<a href="mailto:NumPy-Discussion@scipy.org">NumPy-Discussion@scipy.org</a><br>
<a href="http://mail.scipy.org/mailman/listinfo/numpy-discussion" target="_blank">http://mail.scipy.org/mailman/listinfo/numpy-discussion</a><br>
<br></blockquote></div><br>