Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the biggest numbers out from huge amount of numbers?

I'd like to get the largest 100 elements out from a list of at least 100000000 numbers.

I could sort the entire list and just take the last 100 elements from the sorted list, but that would be very expensive in terms of both memory and time.

Is there any existing easy, pythonic way of doing this?

What I want is following function instead of a pure sort. Actually I don't want waste time to sort the elements I don't care.

For example, this is the function I'd like to have:

getSortedElements(100, lambda x,y:cmp(x,y))

Note this requirement is only for performance perspective.

like image 779
limi Avatar asked Aug 02 '09 13:08

limi


1 Answers

The heapq module in the standard library offers the nlargest() function to do this:

top100 = heapq.nlargest(100, iterable [,key])

It won't sort the entire list, so you won't waste time on the elements you don't need.

like image 78
Ned Batchelder Avatar answered Sep 25 '22 21:09

Ned Batchelder