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.
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.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With