-
-
Save jgomo3/70df1704a5c242be8b17ab4198bbdf70 to your computer and use it in GitHub Desktop.
import heapq | |
def top(n, col, key=None): | |
return heapq.nsmallest(n, col, key) | |
# Alternative, descent only for small n. | |
from itertools import count | |
def top_small_n(n, col, key=None): | |
podium = [] | |
for i, e in enumerate(col): | |
if i < n: | |
podium.append(e) | |
else: | |
podium[-1] = e | |
if i >= n: | |
podium.sort(key=key) | |
if i < n: | |
podium.sort(key=key) | |
return podium |
jackboot7
commented
Feb 20, 2020
@jackboot7: Good! Really straightforward.
As the docs of heapq.nsmallest
says:
The latter two (nsmallest and nlargest) functions perform best for smaller values of n (the size of the collection). For larger values, it is more efficient to use the sorted() function. Also, when n==1, it is more efficient to use the built-in min() and max() functions. If repeated usage of these functions is required, consider turning the iterable into an actual heap.
And I was thinking, that top_small
function could be the best option for really big size of collections and small n
. Think about it: the top 10 of many millions of something. The function would traverse the collection only once, and sort the podium
of 10 elements, many millions of times. That is still linear, I think.