How to remove item from heap efficiently?

Sven R. Kunze srkunze at mail.de
Fri Jan 8 11:45:11 EST 2016


Thanks for your suggestion.

On 08.01.2016 14:21, srinivas devaki wrote:
> You can create a single heap with primary key as timestamp and
> secondary key as priority, i.e by creating a tuple
> insert the elements into the heap as
> (timestamp, priority)
I think I cannot use that because I need the list sorted by both criteria.
> If there is any underlying meaning for creating 2 heaps. please mention.

I use two heaps because I need to insert arbitrary items fast and remove 
the ones fast which are too old (timestamp) or are next in order (priority).

Basically a task scheduler where tasks can be thrown away once they are 
too long in the queue.

> On Fri, Jan 8, 2016 at 4:22 AM, Sven R. Kunze <srkunze at mail.de> wrote:
>> Hi everybody,
>>
>> suppose, I need items sorted by two criteria (say timestamp and priority).
>> For that purpose, I use two heaps (heapq module):
>>
>> heapA # items sorted by timestamp
>> heapB # items sorted by priority
>>
>> Now my actual problem. When popping an item of heapA (that's the oldest
>> item), I need to remove the very same item from heapB, regardlessly where it
>> is in heapB. And vice versa.
>>
>> Is there a datastructure or a simple trick to achieve that in an efficient
>> matter?
>>
>> Best,
>> Sven
>> --
>> https://mail.python.org/mailman/listinfo/python-list




More information about the Python-list mailing list