Top-rated recipes tagged "datastructures"http://code.activestate.com/recipes/tags/datastructures/top/2017-05-12T10:40:58-07:00ActiveState Code RecipesBrowser 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> groupby() 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> 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> 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> 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> 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> 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> 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> 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> 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> OO Meta-programing: method generation from a template (Tcl) 2001-09-08T10:37:50-07:00Kristoffer Lawsonhttp://code.activestate.com/recipes/users/129886/http://code.activestate.com/recipes/68378-oo-meta-programing-method-generation-from-a-templa/ <p style="color: grey"> Tcl recipe 68378 by <a href="/recipes/users/129886/">Kristoffer Lawson</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>A straightforward routine to allow easy creation of new methods based on a previous template. The implementation language is XOTcl. This could also be considered a showcase for some XOTcl features.</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> Shuffling a list (Tcl) 2001-06-21T16:59:34-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65435-shuffling-a-list/ <p style="color: grey"> Tcl recipe 65435 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>How to randomly shuffle a list</p> Parsing comma separated values (Tcl) 2001-06-21T16:53:14-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65433-parsing-comma-separated-values/ <p style="color: grey"> Tcl recipe 65433 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>How to parse lines containing comma-separated values.</p> Dialect for sort by... then by.. (Tcl) 2001-06-21T16:49:17-07:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65432-dialect-for-sort-by-then-by/ <p style="color: grey"> Tcl recipe 65432 by <a href="/recipes/users/98167/">Jeff Hobbs</a> (<a href="/recipes/tags/datastructures/">datastructures</a>). </p> <p>In a number of applications it is necessary to sort a list after several keys, for example first after a date and then after a name.</p>