在Python中使用heapq获取堆中当前元素的列表?

在Python中使用heapq获取堆中当前元素的列表?,python,class,object,heap,priority-queue,Python,Class,Object,Heap,Priority Queue,我建立这个结构是为了更容易理解和使用 class PriorityQueue: """ Implements a priority queue data structure. """ def __init__(self): self.heap = [] self.count = 0 def push(self, item, priority): entry = (priority, self.count, item) heapq.heappush(

我建立这个结构是为了更容易理解和使用

    class PriorityQueue:
"""
  Implements a priority queue data structure.
"""
def  __init__(self):
    self.heap = []
    self.count = 0

def push(self, item, priority):
    entry = (priority, self.count, item)
    heapq.heappush(self.heap, entry)
    self.count += 1

def pop(self):
    (_, _, item) = heapq.heappop(self.heap)
    return item

def isEmpty(self):
    return len(self.heap) == 0
我想添加一个方法,该方法将返回此类中当前的项目列表,这样它就可以返回列表元素,而不必实际弹出每个项目


有没有这样的方法,或者我必须提取每个元素

您的方法可能如下所示:

def items(self):
    return list(item for _, _, item in self.heap)
这将对堆进行迭代,并构建一个新列表,其中包含对堆中项目的引用,而无需修改堆

请注意,如果项目是可变对象,则修改列表中的项目也将修改堆中的引用项目。这可能是可取的,也可能不是可取的


还要注意,此列表不会按优先级排序。

堆只是一个列表。只需使用
self.heap