Python,按值在PriorityQueue中查找元素

时间:2019-05-02 18:00:15

标签: python find priority-queue

优先级队列PQ存储对

(weight, element),

根据重量排序。 Python的实现是否通过元素值(是否为PQ中的元素)支持PQ中的搜索操作?

PQ = queue.PriorityQueue()
PQ.put((1,2))
PQ.put((5,6))
val = 6
print (val in PQ.queue)       #Nothing has been found

我想PQ中的项目以元组形式存储在PQ.queue中。

感谢您的帮助。

1 个答案:

答案 0 :(得分:1)

尝试一下,

如果要搜索元素:

import queue

PQ = queue.PriorityQueue()
PQ.put((1, 2))
PQ.put((5, 6))
val =  4

while not PQ.empty():
   if val in PQ.get():
     print(True)

如果要搜索元组:

import queue

PQ = queue.PriorityQueue()
PQ.put((1, 2))
PQ.put((5, 6))
val = (5, 6)

while not PQ.empty():
  if val == PQ.get():
     print(True)