Path: csiph.com!news.swapon.de!fu-berlin.de!uni-berlin.de!not-for-mail From: "Sven R. Kunze" Newsgroups: comp.lang.python Subject: Re: How to remove item from heap efficiently? Date: Fri, 8 Jan 2016 17:45:11 +0100 Lines: 37 Message-ID: References: <568EEC40.7070807@mail.de> Mime-Version: 1.0 Content-Type: text/plain; charset=utf-8; format=flowed Content-Transfer-Encoding: 7bit X-Trace: news.uni-berlin.de BI0LryAbJ6SBOqrnuAYNdAwT2N1Q5wYUB09FCe+zUvvw== Return-Path: X-Original-To: python-list@python.org Delivered-To: python-list@mail.python.org X-Spam-Status: OK 0.001 X-Spam-Evidence: '*H*': 1.00; '*S*': 0.00; 'everybody,': 0.07; 'cc:addr:python-list': 0.09; 'subject:How': 0.09; 'thrown': 0.09; 'timestamp': 0.09; 'tuple': 0.09; 'underlying': 0.09; 'jan': 0.11; '(say': 0.16; '2016': 0.16; 'heap': 0.16; 'i.e': 0.16; 'popping': 0.16; 'priority).': 0.16; 'received:io': 0.16; 'received:psf.io': 0.16; 'subject:item': 0.16; 'subject:remove': 0.16; 'suggestion.': 0.16; 'wrote:': 0.16; 'basically': 0.18; 'cc:2**0': 0.20; 'cc:addr:python.org': 0.20; 'cc:no real name:2**0': 0.22; 'am,': 0.23; 'elements': 0.23; 'insert': 0.23; 'header:In-Reply-To:1': 0.24; 'header:User-Agent:1': 0.26; 'fri,': 0.27; 'actual': 0.28; 'url:mailman': 0.30; 'creating': 0.30; 'task': 0.30; 'primary': 0.31; 'url:python': 0.33; 'url:listinfo': 0.34; 'received:10.0': 0.34; 'list': 0.34; 'best,': 0.35; 'ones': 0.35; 'next': 0.35; 'tasks': 0.35; 'item': 0.35; 'problem.': 0.35; 'too': 0.36; 'there': 0.36; 'url:org': 0.36; 'subject:?': 0.36; 'subject:: ': 0.37; 'received:10': 0.37; 'two': 0.37; 'thanks': 0.37; 'subject:from': 0.39; 'url:mail': 0.40; 'where': 0.40; 'received:de': 0.40; 'your': 0.60; 'oldest': 0.66; 'mention.': 0.84 DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/simple; d=mail.de; s=mail201212; t=1452271513; bh=0HSjxqNhKEnRMD+sdSFX/h8pDrvRh9PmuiHFpg+cuFc=; h=Subject:To:References:Cc:From:Date:In-Reply-To:From; b=Ur6SIlKDgI6H5sv0bEf0ose1ElUG6PhvruYd53yQWpYPVIZvzrJFqLdkmtuvpRUuw Zt8WBnSSjlBV+FgkG1qEw1GpjsqWgWx5v18pQ86WBZp6wizoGFbw8FDsLwPZx6dYkk 61JWn2M4JctkgcECBdWuromWV6HHFd6CzrAZ7L00= User-Agent: Mozilla/5.0 (X11; Linux x86_64; rv:38.0) Gecko/20100101 Thunderbird/38.4.0 In-Reply-To: X-purgate: clean X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate-type: clean X-purgate-Ad: Categorized by eleven eXpurgate (R) http://www.eleven.de X-purgate: This mail is considered clean (visit http://www.eleven.de for further information) X-purgate: clean X-purgate-size: 1356 X-purgate-ID: 154282::1452271513-000009F3-5E6E6917/0/0 X-Mailman-Approved-At: Sat, 09 Jan 2016 09:51:08 -0500 X-BeenThere: python-list@python.org X-Mailman-Version: 2.1.20+ Precedence: list List-Id: General discussion list for the Python programming language List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Xref: csiph.com comp.lang.python:101402 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 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