n最大和n最小;堆 python

nlargest and nsmallest ; heapq python

这是出于对 python 中 heapq.py 模块的 nsmallest 和 nlargest 方法的好奇。

我在文档中阅读 here

文档没有说明它是如何在任何可迭代对象上执行此操作的 (nsmalles/nlargest)。

这可能是一个愚蠢的问题,但我可以假设这些方法在内部创建了一个可迭代数据结构堆(可能使用 'heapify' 方法)然后 return n smallest/largest个元素?

只是想证实我的结论。谢谢!

从具有 N 项的可迭代项中查找 n 最小或最大项的算法有点棘手。你看,你没有创建一个 size-N min-heap 来找到最小的项目。

相反,您使用前 n 项创建一个较小的、大小为 n 的最大堆,然后使用序列中的其余项对其重复执行 pushpop 操作.完成后,从堆中弹出项目并 return 它们以相反的顺序。

这个过程需要 O(N log(n)) 时间(注意小的 n),当然只需要 O(n) space。如果n远小于N,比排序切片效率高很多

heapq module contains a pure-Python implementation of this algorithm, though when you import it, you may get a faster version of the code written in C instead (you can read the source for that 也是,但它不太友好,除非你知道 Python C API)。