Popular Python recipes tagged "datastructures"http://code.activestate.com/recipes/langs/python/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>
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>