Python3-优先级队列-使用O(log(n))搜索和删除项目
我在Python3中使用Python3-优先级队列-使用O(log(n))搜索和删除项目,python,python-3.x,queue,priority-queue,Python,Python 3.x,Queue,Priority Queue,我在Python3中使用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):
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)
从队列导入优先级队列
类元素(对象):
定义初始化(self,值:int):
self.val=值
#自定义比较函数(小于或等于)
定义(自身、其他):
“selfother.val
#打印每个元素函数
定义报告(自我):
返回f'PQE:{self.val}'
#用法-
pq=优先级队列()
pq.put(PqElement(v))#添加项目-O(日志(n))
topValue=pq.get()#弹出顶部项目-O(1)
topValue=pq.queue[0]。val#获取最高值-O(1)
我喜欢用O(log(n))搜索和删除优先级队列中的项目
我想知道是否有办法做到这一点
有人能帮忙吗?“搜索和删除项目”为什么要搜索优先级队列?我需要知道优先级队列中是否存在该项目。搜索是可选的,但我需要使用queue进行删除。PriorityQueue
是必需的吗?另外,插入/删除的总数顺序可能是什么?我需要从PriorityQueue
中删除,我不必将此库用于优先级队列,它可以是另一个库,如heapq
程序可以执行多少次插入/删除?我有一种方法,但为了衡量其效率,我需要插入/删除的数量