具有两个优先级 Python 的优先级队列 [英] Priority Queue with two Priorities Python
本文介绍了具有两个优先级 Python 的优先级队列的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在寻找一种允许我给出两个优先级的优先级队列.我希望它只检查第一个值然后检查第二个值这是一些代码
I'm searching for a kind of priority queue which allows me to give two priorites. I want that it just check for the first value then for the second one Here is some Code
import Queue
class Job(object):
def __init__(self, fpriority, spriority, description, iata , hops, cost):
self.fpriority = fpriority
self.spriority = spriority
q = Queue.PriorityQueue()
q.put(Job(2, 5, 'Mid-level job'))
q.put(Job(2, 20, 'Low-level job'))
q.put(Job(1, 20, 'Important job'))
现在我想要元素的以下顺序
now i want the following order of the elements
Important job
Mid_level job
Low_level job
如何用一个队列创建这样的订单?
how can i create such an order with one queue?
推荐答案
class Job(object):
def __init__(self, fpriority, spriority, description, iata , hops, cost):
self.fpriority = fpriority
self.spriority = spriority
def __cmp__(self, other):
'''Comparisons for Python 2.x - it's done differently in 3.x'''
if self.fpriority > other.fpriority:
return 1
elif self.fpriority < other.fpriority:
return -1
else:
if self.spriority > other.spriority:
return 1
elif self.spriority < other.spriority:
return -1
else:
return 0
这篇关于具有两个优先级 Python 的优先级队列的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文