python heapq模块 删除中间某一特定参数

python内的heapq提供heappush,heappop两个方法,然而对于 删除 中间的某个参数没有给出相应的方法:

from heapq import heappush, heappop, _siftdown, _siftup
# heap data structure, (value, key), value is used for sorting and key used for identifying
def heapdelete(heap,i):
    nodeValue = heap[i];leafValue = heap[-1];
    if nodeValue == leafValue:
        heap.pop(-1)
    elif nodeValue <= leafValue: # similar to heappop
        heap[i], heap[-1] = heap[-1], heap[i]
        minimumValue = heap.pop(-1)
        if heap != []:
            _siftup(heap, i)
    else: # similar to heappush
        heap[i], heap[-1] = heap[-1], heap[i]
        minimumValue = heap.pop(-1)
        _siftdown(heap, 0, i)
发布了36 篇原创文章 · 获赞 0 · 访问量 2万+

猜你喜欢

转载自blog.csdn.net/weixin_38102912/article/details/85267321