For one python list perhaps numpy Array , I need to find this list The largest of K Number and its corresponding subscript .
1. A dictionary can be constructed to solve , But there is a lot of code .
2. Use heapq library , The subscript and value of the maximum value can be obtained directly .
import heapq
a = [4,2,6,1,9,9]
# Get subscript , Output is [4, 5, 2]
heapq.nlargest(3, range(len(a)), a.__getitem__)
# Get the value , Output is [9, 9, 6]
heapq.nlargest(3,a)
If you want to take the smallest number , Use nsmallest that will do