Popular Python recipes tagged "meta:requires=heapq"http://code.activestate.com/recipes/langs/python/tags/meta:requires=heapq/2015-09-17T12:41:15-07:00ActiveState Code RecipesPython A* Pathfinding (With Binary Heap) (Python) 2014-08-06T10:03:01-07:00Christian Careagahttp://code.activestate.com/recipes/users/4186639/http://code.activestate.com/recipes/578919-python-a-pathfinding-with-binary-heap/ <p style="color: grey"> Python recipe 578919 by <a href="/recipes/users/4186639/">Christian Careaga</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/binary_search/">binary_search</a>). Revision 3. </p> <p>Implementation of the A-star Pathfinding algorithm in Python, using Binary heap to sort the open list</p> Priority Queue (with deletion) (Python) 2013-12-08T21:12:55-08:00elazarhttp://code.activestate.com/recipes/users/4187847/http://code.activestate.com/recipes/578780-priority-queue-with-deletion/ <p style="color: grey"> Python recipe 578780 by <a href="/recipes/users/4187847/">elazar</a> (<a href="/recipes/tags/artificial_intelligence/">artificial_intelligence</a>, <a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/priority_queue/">priority_queue</a>, <a href="/recipes/tags/queue/">queue</a>). Revision 5. </p> <p>Based on the interface defined in aima-python <a href="http://aima-python.googlecode.com/svn/trunk/utils.py" rel="nofollow">http://aima-python.googlecode.com/svn/trunk/utils.py</a></p> <p>Yields better performance.</p> heap class (Python) 2015-09-17T12:41:15-07:00yotahttp://code.activestate.com/recipes/users/4184815/http://code.activestate.com/recipes/578694-heap-class/ <p style="color: grey"> Python recipe 578694 by <a href="/recipes/users/4184815/">yota</a> (<a href="/recipes/tags/heap/">heap</a>). Revision 2. </p> <p><em>heapq</em> is a nice python module, but the interface is not so clean. I found a one-liner on this <a href="http://metapython.blogspot.de/2010/10/creating-heap-class-in-one-python-line.html">blog</a>, it's as short as possible, but not really pythonic either :) Here is my contribution to a most readable heap class.</p> Simple Dijkstra Algorithm (Python) 2012-06-08T02:20:18-07:00Filippo Squillacehttp://code.activestate.com/recipes/users/4174931/http://code.activestate.com/recipes/578156-simple-dijkstra-algorithm/ <p style="color: grey"> Python recipe 578156 by <a href="/recipes/users/4174931/">Filippo Squillace</a> (<a href="/recipes/tags/graph/">graph</a>). Revision 3. </p> <p>The algorithm uses the priority queue version of Dijkstra and return the distance between the source node and the others nodes d(s,i). </p> Recursively defined, Haskell-style infinite lists (Python) 2012-05-04T14:09:14-07:00John Crichtonhttp://code.activestate.com/recipes/users/4181975/http://code.activestate.com/recipes/578119-recursively-defined-haskell-style-infinite-lists/ <p style="color: grey"> Python recipe 578119 by <a href="/recipes/users/4181975/">John Crichton</a> (<a href="/recipes/tags/decorator/">decorator</a>, <a href="/recipes/tags/functional/">functional</a>, <a href="/recipes/tags/generator/">generator</a>, <a href="/recipes/tags/infinite/">infinite</a>, <a href="/recipes/tags/itertools/">itertools</a>, <a href="/recipes/tags/lazy/">lazy</a>, <a href="/recipes/tags/recursive/">recursive</a>). Revision 2. </p> <p>A decorator to simplify the creation of recursively defined, Haskell-style infinite lists -- ie. recursive generators -- inspired by Raymond Hettinger's "Technique for cyclical iteration" [*]. </p> <p>[*] <a href="http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/" rel="nofollow">http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/</a> </p> Dijkstra shortest path implementation (Python) 2011-10-05T09:06:50-07:00Shao-chuan Wanghttp://code.activestate.com/recipes/users/4168519/http://code.activestate.com/recipes/577892-dijkstra-shortest-path-implementation/ <p style="color: grey"> Python recipe 577892 by <a href="/recipes/users/4168519/">Shao-chuan Wang</a> (<a href="/recipes/tags/dijkstra/">dijkstra</a>, <a href="/recipes/tags/optimization/">optimization</a>, <a href="/recipes/tags/shortest/">shortest</a>). </p> <p>This code snippet is the implementation of Dijkstra's algorithm.</p> Compare algorithms for heapq.smallest (Python) 2011-12-25T23:41:23-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577573-compare-algorithms-for-heapqsmallest/ <p style="color: grey"> Python recipe 577573 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/benchmark/">benchmark</a>, <a href="/recipes/tags/heaps/">heaps</a>, <a href="/recipes/tags/performance/">performance</a>). Revision 3. </p> <p>General purpose technique for counting comparisons in various searching and sorting applications.</p> Queue for managing multiple SIGALRM alarms concurrently (Python) 2012-12-06T18:58:11-08:00Glenn Eychanerhttp://code.activestate.com/recipes/users/4172294/http://code.activestate.com/recipes/577600-queue-for-managing-multiple-sigalrm-alarms-concurr/ <p style="color: grey"> Python recipe 577600 by <a href="/recipes/users/4172294/">Glenn Eychaner</a> (<a href="/recipes/tags/alarm/">alarm</a>, <a href="/recipes/tags/queue/">queue</a>, <a href="/recipes/tags/signal/">signal</a>). Revision 3. </p> <p>In asynchronous code, <em>signal.alarm()</em> is extremely useful for setting and handling timeouts and other timed and periodic tasks. It has a major limitation, however, that only one alarm function and alarm time can be set at a time; setting a new alarm disables the previous alarm. This package uses a <em>heapq</em> to maintain a queue of alarm events, allowing multiple alarm functions and alarm times to be set concurrently.</p> Counter class (Python) 2011-04-17T21:27:05-07:00Praveenhttp://code.activestate.com/recipes/users/4177693/http://code.activestate.com/recipes/577664-counter-class/ <p style="color: grey"> Python recipe 577664 by <a href="/recipes/users/4177693/">Praveen</a> . </p> <p>Bag/multiset class for convenient tallying of hashable objects.</p> An interval mapping data structure (Python) 2010-12-23T12:51:27-08:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/2433284/http://code.activestate.com/recipes/577515-an-interval-mapping-data-structure/ <p style="color: grey"> Python recipe 577515 by <a href="/recipes/users/2433284/">Matteo Dell'Amico</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/interval/">interval</a>, <a href="/recipes/tags/mapping/">mapping</a>). </p> <p>This structure is a kind of dictionary which allows you to map data intervals to values. You can then query the structure for a given point, and it returns the value associated to the interval which contains the point. Boundary values don't need to be an integer.</p> <p>In this version, the excellent <a href="http://pypi.python.org/pypi/blist/">blist</a> library by Daniel Stutzbach is used for efficiency. By using the collections.MutableMapping abstract base class, the whole signature of mappings is supported.</p> Counter class (Python) 2009-06-29T12:24:14-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576611-counter-class/ <p style="color: grey"> Python recipe 576611 by <a href="/recipes/users/178123/">Raymond Hettinger</a> . Revision 11. </p> <p>Bag/multiset class for convenient tallying of hashable objects.</p> Technique for cyclical iteration (Python) 2009-12-03T23:08:38-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576961-technique-for-cyclical-iteration/ <p style="color: grey"> Python recipe 576961 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/concurrency_model/">concurrency_model</a>, <a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>). Revision 7. </p> <p>Solution to the Hamming Number problem. Demonstrates a lazy evaluation evaluation technique using itertools.tee() to feed an iterator into itself. This is a common technique with Haskell. The deferred_output() function is the key technique for implementing a forward reference to the output of the stream.</p> Merge multiple (potentially infinite) sorted inputs into a single sorted output (Python) 2010-04-01T04:54:16-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/577041-merge-multiple-potentially-infinite-sorted-inputs-/ <p style="color: grey"> Python recipe 577041 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/merge/">merge</a>, <a href="/recipes/tags/sort/">sort</a>). Revision 4. </p> <p>Merge a (possibly infinite) number of already sorted inputs (each of possibly infinite length) into a single sorted output.</p> <p>Similar to heapq.merge and sorted(itertools.chain(*iterables)).</p> <p>Like heapq.merge, returns a generator, does not pull the data into memory all at once, and assumes that each of the input iterables is already sorted (smallest to largest).</p> <p>Unlike heapq.merge, accepts an infinite number of input iterables, but requires all of them to come in ascending order (that is, their starting point must come in ascending order).</p> <p>In addition, accepts a <em>key</em> function (like <code>sorted</code>, <code>min</code>, <code>max</code>, etc.)</p> Technique for cyclical iteration II (Python) 2010-01-20T03:54:15-08:00Jakub Wronieckihttp://code.activestate.com/recipes/users/4172837/http://code.activestate.com/recipes/577014-technique-for-cyclical-iteration-ii/ <p style="color: grey"> Python recipe 577014 by <a href="/recipes/users/4172837/">Jakub Wroniecki</a> (<a href="/recipes/tags/cyclic_iterator/">cyclic_iterator</a>, <a href="/recipes/tags/recursive_iterator/">recursive_iterator</a>). </p> <p>Inspired by this recipe: <a href="http://code.activestate.com/recipes/576961/" rel="nofollow">http://code.activestate.com/recipes/576961/</a>, I played a little with the code and came with a more general piece of code implementing recursive iterators.</p> Sorting big files the Python 2.6 way (Python) 2009-05-30T21:51:09-07:00Gabriel Genellinahttp://code.activestate.com/recipes/users/924636/http://code.activestate.com/recipes/576755-sorting-big-files-the-python-26-way/ <p style="color: grey"> Python recipe 576755 by <a href="/recipes/users/924636/">Gabriel Genellina</a> (<a href="/recipes/tags/binary/">binary</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/sort/">sort</a>, <a href="/recipes/tags/sorting/">sorting</a>, <a href="/recipes/tags/text/">text</a>). Revision 3. </p> <p>This is just a rewrite of <a href="http://code.activestate.com/recipes/466302/">Recipe 466302</a> "Sorting big files the Python 2.4 way", taking advantage of heapq.merge, context managers, and other niceties of newer Python versions. It can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p> Handling ties for top largest/smallest elements (Python) 2009-04-07T18:57:35-07:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576712-handling-ties-for-top-largestsmallest-elements/ <p style="color: grey"> Python recipe 576712 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/largest/">largest</a>, <a href="/recipes/tags/smallest/">smallest</a>, <a href="/recipes/tags/top/">top</a>). Revision 8. </p> <p>The heapq module provides efficient functions for getting the top-N smallest and largest elements of an iterable. A caveat of these functions is that if there are ties (i.e. equal elements with respect to the comparison key), some elements may end up in the returned top-N list while some equal others may not:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; nsmallest(3, [4,3,-2,-3,2], key=abs) [-2, 2, 3] </code></pre> <p>Although 3 and -3 are equal with respect to the key function, only one of them is chosen to be returned. For several applications, an all-or-nothing approach with respect to ties is preferable or even required.</p> <p>A new optional boolean parameter 'ties' is proposed to accomodate these cases. If ties=True and the iterable contains more than N elements, the length of the returned sorted list can be lower than N if not all ties at the last position can fit in the list:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; nsmallest(3, [4,3,-2,-3,2], key=abs, ties=True) [-2, 2] </code></pre> LRU and LFU cache decorators (Python) 2010-08-01T01:19:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/498245-lru-and-lfu-cache-decorators/ <p style="color: grey"> Python recipe 498245 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). Revision 6. </p> <p>One-line decorator call adds caching to functions with hashable arguments and no keyword arguments. When the maximum size is reached, the least recently used entry or least frequently used entry is discarded -- appropriate for long-running processes which cannot allow caches to grow without bound. Includes built-in performance instrumentation.</p> Sort extremely large and/or binary files in Python (Python) 2008-02-18T18:46:39-08:00Jeremy Mortishttp://code.activestate.com/recipes/users/4126022/http://code.activestate.com/recipes/546524-sort-extremely-large-andor-binary-files-in-python/ <p style="color: grey"> Python recipe 546524 by <a href="/recipes/users/4126022/">Jeremy Mortis</a> (<a href="/recipes/tags/files/">files</a>). </p> <p>This recipe can be used to sort very large files (millions of records) in Python. No record termination character is required, hence a record may contain embedded binary data, newlines, etc. You can specify how many temporary files to use and where they are located.</p> Iterator merge 2 (Python) 2007-12-07T22:16:05-08:00Jonathan Croninhttp://code.activestate.com/recipes/users/4107146/http://code.activestate.com/recipes/535160-iterator-merge-2/ <p style="color: grey"> Python recipe 535160 by <a href="/recipes/users/4107146/">Jonathan Cronin</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Memory efficient multi-way iterator merge, without using heapq</p> Priority dict: a priority queue with updatable priorities (Python) 2007-06-26T09:20:06-07:00Matteo Dell'Amicohttp://code.activestate.com/recipes/users/4057132/http://code.activestate.com/recipes/522995-priority-dict-a-priority-queue-with-updatable-prio/ <p style="color: grey"> Python recipe 522995 by <a href="/recipes/users/4057132/">Matteo Dell'Amico</a> (<a href="/recipes/tags/search/">search</a>). </p> <p>You may have needed a priority queue with the ability to change the priorities of elements that were in the queue. This recipe solves this problem in an efficient way.</p>