use heapq.nsmallest
(and enumerate
to get the index):
nums = [random.randint(1,1000000) for _ in range(10000)]
import heapq
import operator
heapq.nsmallest(10,enumerate(nums),key=operator.itemgetter(1))
Out[26]:
[(5544, 35),
(1702, 43),
(6547, 227),
(1540, 253),
(4919, 360),
(7993, 445),
(1608, 495),
(5832, 505),
(1388, 716),
(5103, 814)]
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…