For example:
a = [1,5,6,2,3]
result = most_find(a, 3)
reslut
[2,1,4] # the index of 6, 5, 3 in a
I know how to implement this function with complex way....
My question
Is there any built-in function in Python or numpy to deal with it ?
Is there any built-in function in Python or numpy to deal with it?
As a matter of fact, there is! It's called numpy.argsort():
import numpy as np
a = [1, 5, 6, 2, 3]
print(np.array(a).argsort()[-3:][::-1]) # If you want to list the indexes of the 4 greatest numbers, change the -3 to -4, etc.
Output:
[2 1 4]
numpyarray, then the linkedargsortorargpartitionshould be fastest. But if a list, then a list sort method is probably better.