Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/306.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/angularjs/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 我注意到我不能对对象使用PriorityQueue?_Python_Python 3.x_Queue - Fatal编程技术网

Python 我注意到我不能对对象使用PriorityQueue?

Python 我注意到我不能对对象使用PriorityQueue?,python,python-3.x,queue,Python,Python 3.x,Queue,我有一个ADT(PCB又名过程控制块),我想把它们放入一个优先级队列。我怎么做 我已经使用了第二优先级来确保队列的正确顺序。在这里,我可以使PCB具有可比性,但在另一个类中,它可能没有意义?那样的话,我该怎么办 更新 我的代码与发布的代码非常相似 我认为问题在于pcb在这里仍然是不可比较的好吧,就结束这个问题。以下是我所做的: 使ADT具有可比性:实现\uu lt\uu() 如果无法使对象按优先级进行比较,则将(priority,obj)对插入PQ。@larsmans,从代码PriorityQu

我有一个ADT(PCB又名过程控制块),我想把它们放入一个优先级队列。我怎么做

我已经使用了第二优先级来确保队列的正确顺序。在这里,我可以使PCB具有可比性,但在另一个类中,它可能没有意义?那样的话,我该怎么办

更新

我的代码与发布的代码非常相似


我认为问题在于pcb在这里仍然是不可比较的

好吧,就结束这个问题。以下是我所做的:

使ADT具有可比性:实现
\uu lt\uu()


如果无法使对象按优先级进行比较,则将
(priority,obj)
对插入PQ。@larsmans,从代码
PriorityQueue.put(self,(priority,self.counter,item))
我已经做了一些事情similar@JiewMeng:如果优先级和计数器总是可比较的,并且没有两个计数器具有相同的值,那么整个三元组都是可比较的。@larsman,我猜Python不能假设是这样的?与此同时,我实现了
\uuult\uuuu
,使事情现在就可以正常工作。仍然有兴趣知道,如果由于某些原因,对类的所有比较逻辑都不符合逻辑,我该怎么办?链接中给出的示例代码应该保证元组前2个元素的唯一性,但python仍然抱怨ADT不可排序。我想这就是Python3的工作方式,我忘了我刚才在哪里读过它…
f=open('.bashrc');g=打开('.vimrc');(1,f)<(2,g)
在Python 3.1.3上运行良好,尽管文件对象是无序的。我想你的程序还有其他缺陷。如果您不想更改类别,例如a
PrioritizedPCB
,也可以使用自定义的
\uu lt\uu
将PCB对象包装为对象类型。
class PCB:
    ...

# in my class extending `PriorityQueue`
PriorityQueue.put(self, (priority, self.counter, pcb))
def __lt__(self, other):
    selfPriority = (self.priority, self.pid)
    otherPriority = (other.priority, other.pid)
    return selfPriority < otherPriority
jiewmeng@JM:~$ python3.2
Python 3.2.2 (default, Sep  5 2011, 21:17:14) 
[GCC 4.6.1] on linux2
Type "help", "copyright", "credits" or "license" for more information.
>>> class Test:
...     def __init__(self, name):
...             self.name = name
... 
>>> from queue import PriorityQueue
>>> q = PriorityQueue()

# duplicate priorities triggering unorderable error
>>> q.put((2, Test("test1")))
>>> q.put((1, Test("test1")))
>>> q.put((3, Test("test1")))
>>> q.put((3, Test("test1")))
>>> q.put((3, Test("test2")))
>>> while not q.empty():
...     print(q.get().name)
... 
Traceback (most recent call last):
  File "<stdin>", line 2, in <module>
  File "/usr/lib/python3.2/queue.py", line 195, in get
    item = self._get()
  File "/usr/lib/python3.2/queue.py", line 245, in _get
    return heappop(self.queue)
TypeError: unorderable types: Test() < Test()

# unique priority fields thus avoiding the problem
>>> q = PriorityQueue()
>>> q.put((3, Test("test1")))
>>> q.put((5, Test("test5")))

>>> while not q.empty():
...     print(q.get()[1].name)
... 
test1
test5