You could combine a priority queue with a set: import heapq class PrioritySet(object): def __init__(self): self. Heap = self. Set = set() def add(self, d, pri): if not d in self.
Set: heapq. Heappush(self. Heap, (pri, d)) self.set.
Add(d) def get(self): pri, d = heapq. Heappop(self. Heap) self.set.
Remove(d) return d This uses the priority queue specified in one of your linked questions. I don't know if this is what you want, but it's rather easy to add a set to any kind of queue this way.
Well here's one way to do it. I basically started from how they defined PriorityQueue in Queue. Py and added a set into it to keep track of unique keys: from Queue import PriorityQueue import heapq class UniquePriorityQueue(PriorityQueue): def _init(self, maxsize): # print 'init' PriorityQueue.
_init(self, maxsize) self. Values = set() def _put(self, item, heappush=heapq. Heappush): # print 'put',item if item1 not in self.
Values: print 'uniq',item1 self.values. Add(item1) PriorityQueue. _put(self, item, heappush) else: print 'dupe',item1 def _get(self, heappop=heapq.
Heappop): # print 'get' item = PriorityQueue. _get(self, heappop) # print 'got',item self.values. Remove(item1) return item if __name__=='__main__': you = UniquePriorityQueue() u.
Put((0.2, 'foo')) u. Put((0.3, 'bar')) u. Put((0.1, 'baz')) u.
Put((0.4, 'foo')) while not u.empty(): item = u. Get_nowait() print item Boaz Yaniv beat me to the punch by a few minutes, but I figured I'd post mine too as it supports the full interface of PriorityQueue. I left some print statements uncommented, but commented out the ones I put in while debugging it.
;).
Thanks for your answer. Didn’t know the methods _init, _put and _get yet, but they are really practical when extending a queue. And as you both used sets, I am convinced now that those are the right way to go ;) – Aufziehvogel May 14 at 15:31.
There is no such thing as a "most efficient priority queue implementation" in any language. A priority queue is all about trade-offs. If you are dealing with data with special properties (such as bounded data), then you can achieve O(1) insertion and O(1) findMin+deleteMin time.
You can only do this with certain kinds of data because otherwise you could abuse your priority queue to violate the O(N log(N)) bound on sorting. To implement any queue in any language, all you need is to define the insert(value) and extractMin() -> value operations. If you only care about which of the two you referenced are more efficient (the heapq-based code from http://docs.python.org/library/heapq.html#priority-queue-implementation-notes which you included above, versus Queue.
There doesn't seem to be any easily-findable discussion on the web as to what Queue. As we can see, Queue. PriorityQueue is also using heapq as an underlying mechanism.
Therefore they are equally bad (asymptotically speaking). PriorityQueue may allow for parallel queries, so I would wager that it might have a very slightly constant-factor more of overhead. But because you know the underlying implementation (and asymptotic behavior) must be the same, the simplest way would simply be to run them on the same large dataset.
(Do note that Queue. PriorityQueue does not seem to have a way to remove entries, while heapq does. However this is a double-edged sword: Good priority queue implementations might possibly allow you to delete elements in O(1) or O(log(N)) time, but if you use the remove_task function you mention, and let those zombie tasks accumulate in your queue because you aren't extracting them off the min, then you will see asymptotic slowdown which you wouldn't otherwise see.
Of course, you couldn't do this with Queue.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.