Priority Queue With Two Priorities 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 Q
Solution 1:
Just use the (fpriority, spriority)
tuple as the priority. That'll do the sorting you want (compare first, then second to break ties).
Solution 2:
classJob(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:
return1elif self.fpriority < other.fpriority:
return -1else:
if self.spriority > other.spriority:
return1elif self.spriority < other.spriority:
return -1else:
return0
Solution 3:
Using NPE's strategy - a tuple as the queue priority, the tuple being (fpriority, spriority)
:
import Queue
classJob(object):
def__init__(self, fpriority, spriority, description='blah', iata='foo' , hops='ample', cost='free pitchers'):
self.fpriority = fpriority
self.spriority = spriority
self.description = description
@propertydefpriority(self):
return (self.fpriority, self.spriority)
def__str__(self):
return self.description
q = Queue.PriorityQueue()
second = Job(2, 5, 'Mid-level job')
third = Job(2, 20, 'Low-level job')
first = Job(1, 20, 'Important job')
q.put((second.priority, second))
q.put((third.priority, third))
q.put((first.priority, first))
while q.unfinished_tasks:
task = q.get()
print task, task[1]
q.task_done()
>>>
((1, 20), <__main__.Job object at 0x02A8F270>) Important job
((2, 5), <__main__.Job object at 0x02A8F230>) Mid-level job
((2, 20), <__main__.Job object at 0x02A8F250>) Low-level job
>>>
This should work for any number of items in a priority tuple.
>>>>>>t = [(1,2),(1,1),(2,2),(2,1),(1,3)]>>>sorted(t)
[(1, 1), (1, 2), (1, 3), (2, 1), (2, 2)]
>>>t = [(2,2,67),(1,2,3),(1,1,0),(2,2,1),(2,1,78),(1,3,78),(1,2,2),(1,2,1),(1,1,6),(2,1,32)]>>>sorted(t)
[(1, 1, 0), (1, 1, 6), (1, 2, 1), (1, 2, 2), (1, 2, 3), (1, 3, 78), (2, 1, 32), (2, 1, 78), (2, 2, 1), (2, 2, 67)]
>>>
Solution 4:
For python3
, priority queue with two priorities can be implemented like this:
from queue import PriorityQueue
classCustomPriorityQueueItem(object):
def__init__(self, data, first_priority, second_priority):
self.first_priority = first_priority
self.second_priority = second_priority
self.data = data
def__lt__(self, other):
if self.first_priority < other.first_priority:
returnTrueelif self.first_priority == other.first_priority:
if self.second_priority < other.second_priority:
returnTrueelse:
returnFalseelse:
returnFalsedef__eq__(self, other):
if self.first_priority == other.first_priority and self.second_priority == other.second_priority:
returnTrueelse:
returnFalsedef__str__(self):
returnstr(self.data)
def__repr__(self):
return self.data
classCustomData:
def__init__(self, name, description):
self.name = name
self.description = description
def__str__(self):
return self.name + ": " + self.description
# test dataif __name__ == "__main__":
q = PriorityQueue()
obj_arr = [
CustomData('name-1', 'description-1'), # this should come 6th - (3, 2)
CustomData('name-2', 'description-2'), # this should come 5st - (3, 1)
CustomData('name-3', 'description-3'), # this should come 2nd - (1, 1)
CustomData('name-4', 'description-4'), # this should come 1th - (1, 2)
CustomData('name-5', 'description-5'), # this should come 3rd - (2, 1)
CustomData('name-6', 'description-6') # this should come 4th - (2, 2)
]
priorities = [(3, 2), (3, 1), (1, 2), (1, 1), (2, 1), (2, 2)]
itr = 0for i inrange(len(obj_arr)):
first_priority = i + 1
second_priority = itr % 2
q.put(CustomPriorityQueueItem(
obj_arr[i], priorities[itr][0], priorities[itr][1]))
itr += 1whilenot q.empty():
data = q.get()
print(str(data))
Output:name-4: description-4name-3: description-3name-5: description-5name-6: description-6name-2: description-2name-1: description-1
Post a Comment for "Priority Queue With Two Priorities Python"