It's difficult to tell what is being asked here. This question is ambiguous, vague, incomplete, overly broad, or rhetorical and cannot be reasonably answered in its current form. For help clarifying this question so that it can be reopened, visit the help center。
9年前关闭。
我无法编辑或排序列表。我怎样才能得到那个索引?
9年前关闭。
我无法编辑或排序列表。我怎样才能得到那个索引?
最佳答案
heapq模块提供了一个nlargest函数,可以有效地查找列表中n个最大的元素:
>>> from heapq import nlargest
>>> items = [100, 300, 200, 400]
>>> indexes = [0, 1, 2, 3]
>>> nlargest(2, indexes, key=lambda i: items[i])
[3, 1]
关于python - Python列表中第n大项的索引,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/2946861/