Popular Python recipes tagged "meta:requires=bisect"http://code.activestate.com/recipes/langs/python/tags/meta:requires=bisect/2013-03-06T17:33:32-08:00ActiveState Code RecipesSortedCollection (Python) 2010-09-01T02:12:33-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577197-sortedcollection/ <p style="color: grey"> Python recipe 577197 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/bisect/">bisect</a>, <a href="/recipes/tags/collection/">collection</a>, <a href="/recipes/tags/sorted/">sorted</a>). Revision 9. </p> <p>Wraps bisect.bisect() in an easy to use class that supports key-functions and straight-forward search methods.</p> Running median, mean and mode (Python) 2013-03-06T17:33:32-08:00Virgil Stokeshttp://code.activestate.com/recipes/users/4183795/http://code.activestate.com/recipes/578480-running-median-mean-and-mode/ <p style="color: grey"> Python recipe 578480 by <a href="/recipes/users/4183795/">Virgil Stokes</a> (<a href="/recipes/tags/mean/">mean</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/mode/">mode</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/running/">running</a>). </p> <p>The following is a small contribution that I hope can be useful to Python programmers for the calculation of the running median, mean and mode. It is important to note that all the "running" calculations are done for full windows. Here is a simple example:</p> <p>y = [1, 2, 3, 3, 1, 4], with a sliding window of size = 3 for the running estimations, means = 2, 2.6667, 2.3333, 2.6667 medians = 2, 3, 3, 3 modes = 1, 3, 3, 1</p> Efficient Running Median using an Indexable Skiplist (Python) 2010-02-07T14:40:03-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576930-efficient-running-median-using-an-indexable-skipli/ <p style="color: grey"> Python recipe 576930 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/advanced_data_structure/">advanced_data_structure</a>, <a href="/recipes/tags/container/">container</a>, <a href="/recipes/tags/median/">median</a>, <a href="/recipes/tags/new_algorithm/">new_algorithm</a>, <a href="/recipes/tags/skiplist/">skiplist</a>, <a href="/recipes/tags/sliding_window/">sliding_window</a>). Revision 10. </p> <p>Maintains sorted data as new elements are added and old one removed as a sliding window advances over a stream of data. Also gives fast indexed access to value. Running time per median update is proportional to the log of the window size.</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> Weighted random choice (Python) 2010-08-19T08:40:38-07:00Carlos Valientehttp://code.activestate.com/recipes/users/4174637/http://code.activestate.com/recipes/577363-weighted-random-choice/ <p style="color: grey"> Python recipe 577363 by <a href="/recipes/users/4174637/">Carlos Valiente</a> (<a href="/recipes/tags/random/">random</a>, <a href="/recipes/tags/sequence/">sequence</a>). </p> <p>This function returns a random element from a sequence. The probability for each element in the sequence to be selected can be weighted by a user-provided callable</p> Sorted dictionary (Python) 2010-01-20T17:11:59-08:00Jan Kaliszewskihttp://code.activestate.com/recipes/users/4172762/http://code.activestate.com/recipes/576998-sorted-dictionary/ <p style="color: grey"> Python recipe 576998 by <a href="/recipes/users/4172762/">Jan Kaliszewski</a> (<a href="/recipes/tags/collections/">collections</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/sorted/">sorted</a>, <a href="/recipes/tags/sorteddict/">sorteddict</a>). Revision 29. </p> <p>A simple implementation of a dictionary which always (when applicable) returns keys, values, items (key-value pairs) sorted by keys (inserting/removing order doesn't matter and only keys are important; so please note that it is something different than OrderedDict in Python 3.1/2.7 or Django's SortedDict).</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> Weighted random choice (Python) 2008-07-22T06:11:19-07:00Facundo Batistahttp://code.activestate.com/recipes/users/2016392/http://code.activestate.com/recipes/576370-weighted-random-choice/ <p style="color: grey"> Python recipe 576370 by <a href="/recipes/users/2016392/">Facundo Batista</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Choices randomly an element from a list, not uniformly, but using a given weight for each element.</p> Weighted choice (Python) 2006-10-30T13:48:55-08:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/498229-weighted-choice/ <p style="color: grey"> Python recipe 498229 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>To choose among a list of objects using a specified frequency distribution.</p> PyDbLite, a small in-memory database engine (Python) 2011-07-18T19:36:04-07:00Pierre Quentelhttp://code.activestate.com/recipes/users/1552957/http://code.activestate.com/recipes/496770-pydblite-a-small-in-memory-database-engine/ <p style="color: grey"> Python recipe 496770 by <a href="/recipes/users/1552957/">Pierre Quentel</a> (<a href="/recipes/tags/database/">database</a>). Revision 4. </p> <p>A small, fast, in-memory database management program</p> <p>The database object supports the iterator protocol, so that requests can be expressed with list comprehensions or generator expressions instead of SQL. The equivalent of :</p> <pre class="prettyprint"><code>cursor.execute("SELECT name FROM table WHERE age=30") rows = cursor.fetchall() </code></pre> <p>is :</p> <pre class="prettyprint"><code>rows = table(age=30) </code></pre> <p>The module stores data in a cPickled file. Records are indexed by a unique record identifier, that can be used for direct access. Since operations are processed in memory they are extremely fast, nearly as fast as SQLite in the few tests I made, and MUCH faster than other pure-Python modules such as Gadfly or KirbyBase. An index can be created on a field to even speed up selections</p> <p>Concurrency control is supported by a version number set for each record</p> <p>Complete documentation is <a href="http://www.pydblite.net">here</a></p> Iterator Algebra Implementations of Relational Join Algorithms (Python) 2006-04-26T10:55:01-07:00Jim Bakerhttp://code.activestate.com/recipes/users/2810167/http://code.activestate.com/recipes/492216-iterator-algebra-implementations-of-relational-joi/ <p style="color: grey"> Python recipe 492216 by <a href="/recipes/users/2810167/">Jim Baker</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Implements the three standard relational join algorithms: nested loops join, hash join, and merge join, using the iterator algebra support in Python 2.4. This recipe presents code that can be used for inner join, left outer join, full outer join, and semijoins. The nested loops join supports a theta join.</p> An interval mapping data structure (Python) 2006-01-18T08:09:18-08:00Nicolas Lehuenhttp://code.activestate.com/recipes/users/1599156/http://code.activestate.com/recipes/457411-an-interval-mapping-data-structure/ <p style="color: grey"> Python recipe 457411 by <a href="/recipes/users/1599156/">Nicolas Lehuen</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </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 ; indeed in the unit test I use a datetime object.</p> Unique stable-unstable (Python) 2005-07-27T18:23:55-07:00bearophile -http://code.activestate.com/recipes/users/2403049/http://code.activestate.com/recipes/438599-unique-stable-unstable/ <p style="color: grey"> Python recipe 438599 by <a href="/recipes/users/2403049/">bearophile -</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 3. </p> <p>Python needs a unique function, this is my best try so far. It uses the faster algorithm (among five) for each situation.</p> Binary Search with the bisect Module (Python) 2005-02-22T15:27:36-08:00Chris Perkinshttp://code.activestate.com/recipes/users/113957/http://code.activestate.com/recipes/389169-binary-search-with-the-bisect-module/ <p style="color: grey"> Python recipe 389169 by <a href="/recipes/users/113957/">Chris Perkins</a> (<a href="/recipes/tags/shortcuts/">shortcuts</a>). </p> <p>Writing a binary search algorithm is surprisingly error-prone. The solution: trick the built-in bisect module into doing it for you.</p> <p>The documentation for bisect says that it works on lists, but it really works on anything with a __getitem__ method. You can exploit this fact to make bisect work in ways that you may not have thought of.</p> <p>Example: Using a library that controls a digital video camera, I wanted to do a poor-man's auto-exposure. The goal is to find the exposure time, in milliseconds, that makes the mean pixel value about 128 (out of 0 to 255).</p> <p>The trick is to create an object that "looks like" a list to the bisect module.</p> Implementation of sets using sorted lists (Python) 2007-05-19T07:07:23-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/230113-implementation-of-sets-using-sorted-lists/ <p style="color: grey"> Python recipe 230113 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Inspired by Py2.3's TimSort, this implementation of sets.py uses sorted lists instead of dictionaries. For clumped data patterns, the set operations can be super-efficient (for example, two sets can be determined to be disjoint with only O(n) comparisons). Also note, that the set elements are <em>not</em> required to be hashable; this provides a great deal more freedom than dictionary based implementations.</p> Rating class with mapping interface (Python) 2004-01-14T09:10:42-08:00Dmitry Vasilievhttp://code.activestate.com/recipes/users/1571302/http://code.activestate.com/recipes/265879-rating-class-with-mapping-interface/ <p style="color: grey"> Python recipe 265879 by <a href="/recipes/users/1571302/">Dmitry Vasiliev</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). Revision 2. </p> <p>Rating with items sorted by value and accessed by key or rating index.</p> Priority queue (Python) 2001-09-24T10:03:00-07:00Sébastien Keimhttp://code.activestate.com/recipes/users/131730/http://code.activestate.com/recipes/68435-priority-queue/ <p style="color: grey"> Python recipe 68435 by <a href="/recipes/users/131730/">Sébastien Keim</a> (<a href="/recipes/tags/algorithms/">algorithms</a>). </p> <p>Priority queues are a kind of container that allows specification of the relative order of the data.</p> Binary search and insert in Python (Python) 2001-04-25T05:09:37-07:00Noah Spurrierhttp://code.activestate.com/recipes/users/103276/http://code.activestate.com/recipes/54159-binary-search-and-insert-in-python/ <p style="color: grey"> Python recipe 54159 by <a href="/recipes/users/103276/">Noah Spurrier</a> (<a href="/recipes/tags/search/">search</a>). Revision 3. </p> <p>This demonstrates a simple binary search through sorted data. A binary search is a basic algorithm provided by bisect in Python. The binary search can be summarized by two lines of code: list.sort() item_insert_point = bisect.bisect (list, item)</p>