Most viewed recipes tagged "datastructures"http://code.activestate.com/recipes/tags/datastructures/views/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>
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>
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>
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>
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>
Sending mail with attachments (Tcl)
2003-02-23T22:42:28-08:00Jeff Hobbshttp://code.activestate.com/recipes/users/98167/http://code.activestate.com/recipes/65434-sending-mail-with-attachments/
<p style="color: grey">
Tcl
recipe 65434
by <a href="/recipes/users/98167/">Jeff Hobbs</a>
(<a href="/recipes/tags/datastructures/">datastructures</a>).
Revision 2.
</p>
<p>How to send an email using the SMTP and MIME packages in tcllib (part of ActiveTcl).</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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>
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>