且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

假设n>如何从n个数字的列表中找到k个最大数字. ķ

更新时间:2023-02-09 22:25:56

Python包含所有电池-使用

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