更新时间:2023-02-09 22:25:56
Python has all batteries included - use heapq
module :)
from heapq import nlargest
data = [0.5, 0.7, 0.3, 0.3, 0.3, 0.4, 0.5]
print nlargest(3, data)
比使用整个堆排序还快,因为它使用了部分堆排序
it's also faster than sorting the whole array, because it's using partial heapsort