python - Python 3 - 优先队列 - 使用 O(log(n)) 按值搜索和删除项目
问题描述
我正在使用这样priority queue
的python 3实现a-queue.PriorityQueue
from queue import PriorityQueue
class PqElement(object):
def __init__(self, value: int):
self.val = value
#Custom Compare Function (less than or equsal)
def __lt__(self, other):
"""self < obj."""
return self.val > other.val
#Print each element function
def __repr__(self):
return f'PQE:{self.val}'
#Usage-
pq = PriorityQueue()
pq.put(PqElement(v)) # Add Item - O(Log(n))
topValue = pq.get() # Pop top item - O(1)
topValue = pq.queue[0].val # Get top value - O(1)
pqSize = pq.qsize() # Provide Queue Size - O(1)
isEmpty = pq.empty() # Is PQ is empty
我喜欢使用 O(log(n)) 搜索和删除优先级队列中的项目。
我想知道是否有任何方法可以做到这一点?
有人可以帮忙吗?
解决方案
推荐阅读
- python-3.x - sqlalchemy.event.listen 未找到事件“after_create”
- javascript - 如何使用 JS 配置文件和 Web 目标创建 npm 库?
- angular - 单击一个按钮会触发其他具有相同事件功能的按钮
- python - 在 Pandas 上创建变量时出错 - 传递的项目数错误
- sql - 使用季度数据查找每位员工的年度 MAX 和 MIN
- sql - 如何在postgres中显示哈希数组中的键?
- css - maven 会创建 css 文件的副本吗?
- ios - 在使用主题获得响应后将数据从父 VC 传递到子 VC
- html - 行全宽中的引导跨度元素
- macos - 如何在 Safari Web Inspector 中查看导致 'safari-resource:/ErrorPage.html' (HTTP 403) 的初始 Web 请求?