heapq模块提供了一下的功能函数:
-
1.heapq.
heappush
(
heap,
item
)
-
向堆中添加元素,保持堆结构不变。
-
heapq.
heappop
(
heap
)
删除并返回堆中最小的元素,保持堆结构不变。如果堆是空的,抛出异常。
-
heapq.
heappushpop
(
heap,
item
)
向堆中添加元素,同时删除并返回最小的元素。
-
heapq.
heapify
(
x
)
把列表转化为堆
-
heapq.
heapreplace
(
heap,
item
)
删除并返回堆中最小的元素,然后向堆添加元素。如果堆为空,抛出异常。
-
heapq.
merge
(
*iterables
)
-
Merge multiple sorted inputs into a single sorted output (for example, merge timestamped entries from multiple log files). Returns an iterator over the sorted values.
Similar to sorted(itertools.chain(*iterables)) but returns an iterable, does not pull the data into memory all at once, and assumes that each of the input streams is already sorted (smallest to largest).
New in version 2.6.
-
heapq.
nlargest
(
n,
iterable
[,
key
]
)
返回堆中n个最大的元素
-
返回堆中n个最小的元素
-
-
heapq. nsmallest ( n, iterable [, key ] )
heapq.
nsmallest
(
n,
iterable
[,
key
]
)
返回堆中n个最小的元素