Popular recipes tagged "datastructures"http://code.activestate.com/recipes/tags/datastructures/2017-05-12T10:40:58-07:00ActiveState Code Recipesgroupby() For Unsorted Input (Python) 2017-05-12T10:40:58-07:00Alfehttp://code.activestate.com/recipes/users/4182236/http://code.activestate.com/recipes/580800-groupby-for-unsorted-input/ <p style="color: grey"> Python recipe 580800 by <a href="/recipes/users/4182236/">Alfe</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/generators/">generators</a>, <a href="/recipes/tags/grouping/">grouping</a>, <a href="/recipes/tags/lazy/">lazy</a>). </p> <p>We all know the <code>groupby()</code> which is available in the <code>itertools</code> standard module. This one yields groups of consecutive elements in the input which are meant to be together in one group. For non-consecutive elements this will yield more than one group for the same key.</p> <p>So effectively, <code>groupby()</code> only reformats a flat list into bunches of elements from that list without reordering anything. In practice this means that for input sorted by key this works perfect, but for unsorted input it might yield several groups for the same key (with groups for other keys in between). Typically needed, though, is a grouping with reordering if necessary.</p> <p>I implemented a likewise lazy function (yielding generators) which also accepts ungrouped input.</p> Finding the sizes of various Python data types (Python) 2016-04-28T18:28:59-07:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580655-finding-the-sizes-of-various-python-data-types/ <p style="color: grey"> Python recipe 580655 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/data/">data</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/memory/">memory</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/system/">system</a>, <a href="/recipes/tags/type/">type</a>). </p> <p>This recipe shows how to find the sizes of various common data types in Python, both built-in and user-defined. It uses the sys.getsizeof() function and also discusses a few other points of interest.</p> defdict (Python) 2015-10-15T15:45:16-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/579113-defdict/ <p style="color: grey"> Python recipe 579113 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/dictionary/">dictionary</a>). </p> <p>default dictionary with collision function to handle key collisions.</p> Bag class (Python) 2013-07-19T21:08:46-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/578611-bag-class/ <p style="color: grey"> Python recipe 578611 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). Revision 8. </p> <p>A bag: a set-like container that simply counts the number of same items held within it. </p> graph (Python) 2015-10-15T21:33:32-07:00userhttp://code.activestate.com/recipes/users/4187240/http://code.activestate.com/recipes/578615-graph/ <p style="color: grey"> Python recipe 578615 by <a href="/recipes/users/4187240/">user</a> (<a href="/recipes/tags/abstract_base_class/">abstract_base_class</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/graph/">graph</a>). Revision 4. </p> <p>Abstract Graph Class: A Graph is a very abstract and powerful data structure to hold many different kinds of data relationships. </p> PseudoStruct (Python) 2012-11-25T03:43:06-08:00Matthew Zipayhttp://code.activestate.com/recipes/users/4183355/http://code.activestate.com/recipes/578349-pseudostruct/ <p style="color: grey"> Python recipe 578349 by <a href="/recipes/users/4183355/">Matthew Zipay</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe for a Python "data object." It is similar in function to namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) and recordtype (<a href="http://code.activestate.com/recipes/576555-records/" rel="nofollow">http://code.activestate.com/recipes/576555-records/</a>) in that it is a simple container for data, but is designed to meet three specific goals:</p> <ol> <li>Easy to subclass data objects.</li> <li>Get/set speed comparable to a simple class.</li> <li>Minimal memory consumption per instance.</li> </ol> Restricted dictionary (Python) 2012-02-16T21:39:02-08:00arnoqueshttp://code.activestate.com/recipes/users/4180947/http://code.activestate.com/recipes/578042-restricted-dictionary/ <p style="color: grey"> Python recipe 578042 by <a href="/recipes/users/4180947/">arnoques</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>It's a dictionary that's restricted to a tuple of allowed keys. Any attempt to set an invalid key raises an error.</p> NamedList (Python) 2012-02-16T18:38:54-08:00Sergey Shepelevhttp://code.activestate.com/recipes/users/4180945/http://code.activestate.com/recipes/578041-namedlist/ <p style="color: grey"> Python recipe 578041 by <a href="/recipes/users/4180945/">Sergey Shepelev</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/mutable/">mutable</a>). </p> <p>Fast lightweight attribute style access to list. I think this is closest to mutable C struct type.</p> <p>Same as collections.namedtuple but subclasses list, so fields may be modified. Same as popular Record class but smaller and faster. <a href="http://code.activestate.com/recipes/502237-simple-record-aka-struct-type/" rel="nofollow">http://code.activestate.com/recipes/502237-simple-record-aka-struct-type/</a></p> vector (Python) 2012-01-09T05:35:39-08:00Stephen Chappellhttp://code.activestate.com/recipes/users/2608421/http://code.activestate.com/recipes/578006-vector/ <p style="color: grey"> Python recipe 578006 by <a href="/recipes/users/2608421/">Stephen Chappell</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/library/">library</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Needed by <a href="http://code.activestate.com/recipes/578004/">recipe 578004</a>, this is meant to be a power pure-python-based module for running optimized 2D vector operations with a few possibilities not seen in most vector libraries. Many of the methods are overloaded to provide great versitility in what operations can be performed. To allow for even greater operations, the many methods mays be wrapped with the included <code>autocast</code> method so that even more datatypes can be used in whatever calculations the programmer may desire.</p> Binary Search Tree (C++) 2011-08-08T00:13:44-07:00sivarama sarmahttp://code.activestate.com/recipes/users/4178890/http://code.activestate.com/recipes/577828-binary-search-tree/ <p style="color: grey"> C++ recipe 577828 by <a href="/recipes/users/4178890/">sivarama sarma</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p> Binary Search Tree (C++) 2011-01-26T22:48:06-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577552-binary-search-tree/ <p style="color: grey"> C++ recipe 577552 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Binary Search Tree.</p> Huffman Data Compression (C++) 2010-12-01T02:47:09-08:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/577480-huffman-data-compression/ <p style="color: grey"> C++ recipe 577480 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/algorithm/">algorithm</a>, <a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/data_compression/">data_compression</a>). Revision 2. </p> <p>Huffman encoding (data compression). Usage: huffman -i [input file name] -o [output file name] [-e|d]</p> Accessing cursors by field name (Python) 2010-04-09T22:50:04-07:00Ricardo Araozhttp://code.activestate.com/recipes/users/4173628/http://code.activestate.com/recipes/577186-accessing-cursors-by-field-name/ <p style="color: grey"> Python recipe 577186 by <a href="/recipes/users/4173628/">Ricardo Araoz</a> (<a href="/recipes/tags/cursor/">cursor</a>, <a href="/recipes/tags/database/">database</a>, <a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/field/">field</a>, <a href="/recipes/tags/name/">name</a>). </p> <p>This class allows you to access the rows of a cursor by field name.</p> Browser history data structure (Python) 2009-12-25T17:26:57-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576991-browser-history-data-structure/ <p style="color: grey"> Python recipe 576991 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/datastuctures/">datastuctures</a>). Revision 6. </p> <p>The <code>BrowserHistory</code> class encapsulates the history of moving from location to location, as in Web browsing context; the recipe is not restricted to Web browsing though. See docstrings for more details and usage.</p> <p>The current implementation requires Python 2.6.</p> Records (Python) 2008-11-04T06:52:42-08:00George Sakkishttp://code.activestate.com/recipes/users/2591466/http://code.activestate.com/recipes/576555-records/ <p style="color: grey"> Python recipe 576555 by <a href="/recipes/users/2591466/">George Sakkis</a> (<a href="/recipes/tags/datastructures/">datastructures</a>, <a href="/recipes/tags/namedtuple/">namedtuple</a>, <a href="/recipes/tags/record/">record</a>). </p> <p>This is a recipe similar in functionality and exec-style optimized implementation to the very well received namedtuple (<a href="http://code.activestate.com/recipes/500261/" rel="nofollow">http://code.activestate.com/recipes/500261/</a>) that was included in Python 2.6. The main difference is that <strong>records</strong>, unlike named tuples, are mutable. In addition, fields can have a default value. Instead of subclassing tuple or list, the implementation create a regular class with __slots__.</p> Single Linked List (PHP) 2006-10-09T11:52:45-07:00Andre Wanderley de Souzahttp://code.activestate.com/recipes/users/2634005/http://code.activestate.com/recipes/498103-single-linked-list/ <p style="color: grey"> PHP recipe 498103 by <a href="/recipes/users/2634005/">Andre Wanderley de Souza</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). Revision 10. </p> <p>As `throw new Exception' is just avaliable in php 5, for use php 4 use that call shoud be replaced.</p> Gettok (Tcl) 2003-11-20T07:58:29-08:00Kiko The Kinghttp://code.activestate.com/recipes/users/772309/http://code.activestate.com/recipes/252145-gettok/ <p style="color: grey"> Tcl recipe 252145 by <a href="/recipes/users/772309/">Kiko The King</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). Revision 3. </p> <p>It returns the Xth token in a sentence.. like: i have a sentence divided by : and i want to get the second value sentence: set x "Value 1 : Value 2 : Value 3" [gettok $x 2 :] is returns &lt;b&gt;Value 2&lt;/b&gt; if you use 0 as the number...it will returns the number of tokens in this case... &lt;b&gt;3&lt;/b&gt;</p> Minimal unique procedure for the versionally challanged (Tcl) 2002-09-01T08:35:34-07:00Nir Levyhttp://code.activestate.com/recipes/users/511931/http://code.activestate.com/recipes/147663-minimal-unique-procedure-for-the-versionally-chall/ <p style="color: grey"> Tcl recipe 147663 by <a href="/recipes/users/511931/">Nir Levy</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Altough the latest Tcl verions have a -unique flag for lsort, older verions do not. So for those with older versions here is some nice, fast uinquer. Note that it assumes that the list items do not include the charecter ',' so it should probably be used only with numeric data.</p> Class Inspection (PHP) 2001-12-07T10:50:29-08:00Shane Caraveohttp://code.activestate.com/recipes/users/98233/http://code.activestate.com/recipes/101522-class-inspection/ <p style="color: grey"> PHP recipe 101522 by <a href="/recipes/users/98233/">Shane Caraveo</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>Simple class introspection using php's class functions. This generates a list of classes, and their members and methods.</p> Collection of bit manipulation functions. (Tcl) 2001-09-08T10:01:37-07:00Scott Beasleyhttp://code.activestate.com/recipes/users/129906/http://code.activestate.com/recipes/68376-collection-of-bit-manipulation-functions/ <p style="color: grey"> Tcl recipe 68376 by <a href="/recipes/users/129906/">Scott Beasley</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>A collection of simple bit manipulation functions in pure TCL. Functions supplied: testbit setbit clearbit</p>