![]() What you see above looks from the outside like a list that keeps itself sorted. Unlike sorted(), this implementation is not stable."So, it's like a list that keeps itself sorted?" the skeptical reader may ask at this point. Since you don't care about sort stability that's all you need.Įdit: As noted in the comments and confirmed here, heappush is not stable: Getting at your original elements in that case would be as simple as elem = queue.get().wrapped_elem # third_returned_elem is PriorityElem(my_custom_class3, 30) # second_returned_elem is PriorityElem(my_custom_class2, 10) # first_returned_elem is PriorityElem(my_custom_class1, 10) ![]() Queue.put(PriorityElem(my_custom_class3, 30)) Queue.put(PriorityElem(my_custom_class2, 10)) Queue.put(PriorityElem(my_custom_class1, 10)) Now you can use PriorityQueue like so queue = PriorityQueue() If your elements do not have priorities then it's as simple as: class PriorityElem:ĭef _init_(self, elem_to_wrap, priority): Return self.wrapped_elem.priority < other.wrapped_elem.priority It's as simple as something like this class PriorityElem: So, it is easy to add a standard sort order to a class by defining an _lt_() method The sort routines are guaranteed to use _lt_() when making comparisons between two objects. See MartijnPieters answer for a nicer unique 2nd element.Īll you need is a wrapper class that implements _lt_ in order for PriorityQueue to work correctly. Using def add(prioqueue,prio,item): pushes triplets as items wich have guaranteed distinct 2nd values so our O()-instances are never used as tie-breaker. Using h.put( (1, O('write spec 1')) ) leads to TypeError: '<' not supported between instances of 'O' and 'int'` ![]() # if you forget to declare this, you get an AttributeError # no len() on PrioQueue - we ensure our unique integer via method-param Is stored as member of this method 'add.n' which is incremented on each usage.""" """Adds the 'item' with 'prio' to the 'priorqueue' adding a unique value that So simply add your items as 3rd element in a tuple (Prio, Count, YourElem) when adding to your queue.Ĭontreived example: from queue import PriorityQueue And since no two entry counts are the same, the tuple comparison will never attempt to directly compare two tasks. The entry count serves as a tie-breaker so that two tasks with the same priority are returned in the order they were added. is to store entries as 3-element list including the priority, an entry count, and the task. See priority queue implementation notes - just before the section you quoted (regarding using dataclasses) it tells you how to do it whitout them: Is there a better way than to define _le_ and other comparison methods on my custom class? I find this solution particularly ugly and counter-intuitive, but that might be me. In the documentation of this version of Python, there's no comment on using PriorityQueue for the priorities, not bothering about the "object value" which wouldn't be logical in my case. If the data elements are not comparable, the data can be wrapped in a class that ignores the data item and only compares the priority number: from dataclasses import dataclass, fieldĪlas, I'm using Python 3.6. In Python 3.7's documentation, there's a solution involving dataclasses. In other words, my objects can't be compared at integers, it won't make sense to allow them to be, I just care about the priority. But if two objects have the same priority, I don't mind PriorityQueue.get to return either. I would like to store objects with a given priority. I'm trying to use queue.PriorityQueue in Python 3(.6).
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |