【发布时间】:2022-01-07 16:06:26
【问题描述】:
这应该是模拟优先级队列,但显然它不起作用......
class PriorityQueue:
def __init__(self):
self.elements = []
def add(self, element):
"""
This will add the element to the correct location in the list.
Example: If given a list where the priorities are thus:
1, 1, 1, 1, 2, 2, 2, 3, 5
And you add 5, it will go here:
1, 1, 1, 1, 2, 2, 2, 3, 5, 5
here^
"""
for i in range(0,len(self.elements)):
if element[0] <= self.elements[i][0] and element[1] <= self.elements[i][1]:
self.elements.insert(i,element)
【问题讨论】:
-
它应该如何工作以及它的实际作用是什么?你能举一个使用的例子吗?每个元素的两个部分是什么?
-
"显然它不起作用" 你是如何得出这个结论的?你如何测试它?你得到什么错误信息?什么结果以及它与您的预期有何不同?
-
我怀疑您希望
self.elements成为有序列表。 Python 的优先级队列是使用二叉堆实现的,它不是一个完全有序的结构。有关详细信息,请参阅Wikipedia article about heaps 的“实施”部分。 -
请澄清您的具体问题或提供更多详细信息以准确突出您的需求。正如目前所写的那样,很难准确地说出你在问什么。
标签: python priority-queue