Most viewed recipes tagged "dictionary"http://code.activestate.com/recipes/tags/dictionary/views/2016-05-09T22:24:26-07:00ActiveState Code RecipesOrdered Dictionary for Py2.4 (Python) 2011-04-24T03:20:45-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576693-ordered-dictionary-for-py24/ <p style="color: grey"> Python recipe 576693 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>). Revision 9. </p> <p>Drop-in substitute for Py2.7's new collections.OrderedDict. The recipe has big-oh performance that matches regular dictionaries (amortized O(1) insertion/deletion/lookup and O(n) iteration/repr/copy/equality_testing).</p> Diff Two Dictionaries (Python) 2009-02-05T08:35:07-08:00Michael Shepanskihttp://code.activestate.com/recipes/users/4169100/http://code.activestate.com/recipes/576644-diff-two-dictionaries/ <p style="color: grey"> Python recipe 576644 by <a href="/recipes/users/4169100/">Michael Shepanski</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/diff/">diff</a>, <a href="/recipes/tags/difference/">difference</a>). </p> <p>Diff two dictionaries returning just the differences. If an item is not found, it is represented by the string "&lt;KEYNOTFOUND&gt;". If there is a better way, please share. :)</p> Persistent dict with multiple standard file formats (Python) 2011-09-06T20:01:46-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/576642-persistent-dict-with-multiple-standard-file-format/ <p style="color: grey"> Python recipe 576642 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/dbm/">dbm</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/persistent/">persistent</a>, <a href="/recipes/tags/shelve/">shelve</a>). Revision 10. </p> <p>dbdict: a dbm based on a dict subclass.</p> <p>On open, loads full file into memory. On close, writes full dict to disk (atomically). Supported output file formats: csv, json, and pickle. Input file format automatically discovered.</p> <p>Usable by the shelve module for fast access.</p> Proof-of-concept for a more space-efficient, faster-looping dictionary (Python) 2013-01-17T09:28:24-08:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/578375-proof-of-concept-for-a-more-space-efficient-faster/ <p style="color: grey"> Python recipe 578375 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/compact/">compact</a>, <a href="/recipes/tags/dictionary/">dictionary</a>). Revision 20. </p> <p>Save space and improve iteration speed by moving the hash/key/value entries to a densely packed array keeping only a sparse array of indices. This eliminates wasted space without requiring any algorithmic changes.</p> Recursively print (nested) dictionaries (Python) 2012-04-04T15:20:42-07:00Mauricio Dada Fonseca de Freitashttp://code.activestate.com/recipes/users/4179701/http://code.activestate.com/recipes/578094-recursively-print-nested-dictionaries/ <p style="color: grey"> Python recipe 578094 by <a href="/recipes/users/4179701/">Mauricio Dada Fonseca de Freitas</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/print/">print</a>, <a href="/recipes/tags/recursion/">recursion</a>). </p> <p>Snippet of code that uses recursion to print nested dictionaries. It does not sort the entries!</p> Python add/set attributes to list (Python) 2015-09-29T16:28:46-07:00webby1111http://code.activestate.com/recipes/users/4192908/http://code.activestate.com/recipes/579103-python-addset-attributes-to-list/ <p style="color: grey"> Python recipe 579103 by <a href="/recipes/users/4192908/">webby1111</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/class/">class</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/subclass/">subclass</a>). Revision 3. </p> <h4 id="python-attribute-listhttpsgithubcomwebby1111python-attribute-list"><a href="https://github.com/webby1111/Python-Attribute-List">Python Attribute List</a></h4> <p>Add/set attributes to python lists.</p> <p>A google search for "add attributes to python lists" yields no good stackoverflow answer, hence the need for this.</p> <p>Useful for machine learning stuff where you need labeled feature vectors. </p> <p>This technique can be easily adapted for other built-ins (e.g. int).</p> <h5 id="the-problem">The Problem</h5> <pre class="prettyprint"><code>a = [1, 2, 4, 8] a.x = "Hey!" # AttributeError: 'list' object has no attribute 'x' </code></pre> <h5 id="the-solution">The Solution</h5> <pre class="prettyprint"><code>a = L(1, 2, 4, 8) a.x = "Hey!" print a # [1, 2, 4, 8] print a.x # "Hey!" print len(a) # 4 # You can also do these: a = L( 1, 2, 4, 8 , x="Hey!" ) # [1, 2, 4, 8] a = L( 1, 2, 4, 8 )( x="Hey!" ) # [1, 2, 4, 8] a = L( [1, 2, 4, 8] , x="Hey!" ) # [1, 2, 4, 8] a = L( {1, 2, 4, 8} , x="Hey!" ) # [1, 2, 4, 8] a = L( [2 ** b for b in range(4)] , x="Hey!" ) # [1, 2, 4, 8] a = L( (2 ** b for b in range(4)) , x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ** b for b in range(4) )( x="Hey!" ) # [1, 2, 4, 8] a = L( 2 ) # [2] </code></pre> Create a nested dictionary from os.walk (Python) 2011-09-26T23:38:24-07:00Andrew Clarkhttp://code.activestate.com/recipes/users/4179419/http://code.activestate.com/recipes/577879-create-a-nested-dictionary-from-oswalk/ <p style="color: grey"> Python recipe 577879 by <a href="/recipes/users/4179419/">Andrew Clark</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/os_walk/">os_walk</a>). Revision 2. </p> <p>Creates a nested dictionary that represents a folder structure. Here is an example of what the resulting dictionary might look like:</p> <pre class="prettyprint"><code>{ "root": { "folder2": { "item2": None, "item1": None }, "folder1": { "subfolder1": { "item2": None, "item1": None }, "subfolder2": { "item3": None } } } } </code></pre> Nested contexts -- a chain of mapping objects (Python) 2010-10-25T02:13:37-07:00Raymond Hettingerhttp://code.activestate.com/recipes/users/178123/http://code.activestate.com/recipes/577434-nested-contexts-a-chain-of-mapping-objects/ <p style="color: grey"> Python recipe 577434 by <a href="/recipes/users/178123/">Raymond Hettinger</a> (<a href="/recipes/tags/ast/">ast</a>, <a href="/recipes/tags/chained/">chained</a>, <a href="/recipes/tags/compiler/">compiler</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/inheritance/">inheritance</a>, <a href="/recipes/tags/nested/">nested</a>, <a href="/recipes/tags/nonlocal/">nonlocal</a>, <a href="/recipes/tags/scopes/">scopes</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>Easy to use chain of dictionaries for crafting nested scopes or for a tree of scopes. Useful for analyzing AST nodes, XML nodes or other structures with multiple scopes. Can emulate various chaining styles including static/lexical scoping, dynamic scoping and Python's own globals(), locals(), nested scopes, and writeable nonlocals. Can also model Python's inheritance chains: instance dictionary, class dictionary, and base classes.</p> State Capitals Quiz (Python) 2009-05-28T07:45:06-07:00joedaviscpahttp://code.activestate.com/recipes/users/4170361/http://code.activestate.com/recipes/576783-state-capitals-quiz/ <p style="color: grey"> Python recipe 576783 by <a href="/recipes/users/4170361/">joedaviscpa</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/educational/">educational</a>, <a href="/recipes/tags/random/">random</a>). </p> <p>The program loops through all 50 states asking for the state capital. </p> xml to python dictionary and back (Python) 2012-10-24T00:44:30-07:00kris kvilekvalhttp://code.activestate.com/recipes/users/4178118/http://code.activestate.com/recipes/577722-xml-to-python-dictionary-and-back/ <p style="color: grey"> Python recipe 577722 by <a href="/recipes/users/4178118/">kris kvilekval</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>Simple mapping of XML to python dictionary based on Perl XML::Simple</p> Multimap: Associating multiple values to a key. (Python) 2009-07-08T13:52:49-07:00Jack Trainorhttp://code.activestate.com/recipes/users/4076953/http://code.activestate.com/recipes/576835-multimap-associating-multiple-values-to-a-key/ <p style="color: grey"> Python recipe 576835 by <a href="/recipes/users/4076953/">Jack Trainor</a> (<a href="/recipes/tags/data_structures/">data_structures</a>, <a href="/recipes/tags/defaultdictionary/">defaultdictionary</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/multimap/">multimap</a>). </p> <p>Implementation of multimap based on lists, dicts, or sets using Python 2.5 defaultdict.</p> Sorting a list of dictionary based on a key value (Python) 2008-08-20T03:30:37-07:00riteshnhttp://code.activestate.com/recipes/users/4166555/http://code.activestate.com/recipes/576433-sorting-a-list-of-dictionary-based-on-a-key-value/ <p style="color: grey"> Python recipe 576433 by <a href="/recipes/users/4166555/">riteshn</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/list/">list</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p>Here is a quick and dirty way to sort a list of dictionary based on a key value.</p> Self-contained TWL06 Dictionary Module (515 KB) (Python) 2011-08-10T20:32:03-07:00Michael Foglemanhttp://code.activestate.com/recipes/users/4171845/http://code.activestate.com/recipes/577835-self-contained-twl06-dictionary-module-515-kb/ <p style="color: grey"> Python recipe 577835 by <a href="/recipes/users/4171845/">Michael Fogleman</a> (<a href="/recipes/tags/dawg/">dawg</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/scrabble/">scrabble</a>, <a href="/recipes/tags/trie/">trie</a>, <a href="/recipes/tags/twl06/">twl06</a>, <a href="/recipes/tags/word/">word</a>). Revision 4. </p> <p>A convenient, self-contained, 515 KB Scrabble dictionary module, ideal for use in word games.</p> <p>Functionality:</p> <ul> <li>Check if a word is in the dictionary.</li> <li>Enumerate all words in the dictionary.</li> <li>Determine what letters may appear after a given prefix.</li> <li>Determine what words can be formed by anagramming a set of letters.</li> </ul> <p>Sample usage:</p> <pre class="prettyprint"><code>&gt;&gt;&gt; import twl &gt;&gt;&gt; twl.check('dog') True &gt;&gt;&gt; twl.check('dgo') False &gt;&gt;&gt; words = set(twl.iterator()) &gt;&gt;&gt; len(words) 178691 &gt;&gt;&gt; twl.children('dude') ['$', 'd', 'e', 's'] &gt;&gt;&gt; list(twl.anagram('top')) ['op', 'opt', 'pot', 'to', 'top'] </code></pre> 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> Find Duplicate Files (Python) 2014-10-12T21:14:05-07:00FB36http://code.activestate.com/recipes/users/4172570/http://code.activestate.com/recipes/578950-find-duplicate-files/ <p style="color: grey"> Python recipe 578950 by <a href="/recipes/users/4172570/">FB36</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/directories/">directories</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/disk/">disk</a>, <a href="/recipes/tags/file/">file</a>, <a href="/recipes/tags/files/">files</a>, <a href="/recipes/tags/utility/">utility</a>). </p> <p>Finds duplicate files which have same size and same content in the same directory or two different directories.</p> Priority queue dictionary (Python) 2013-08-25T00:23:12-07:00Nezar Abdennurhttp://code.activestate.com/recipes/users/4187557/http://code.activestate.com/recipes/578643-priority-queue-dictionary/ <p style="color: grey"> Python recipe 578643 by <a href="/recipes/users/4187557/">Nezar Abdennur</a> (<a href="/recipes/tags/algorithms/">algorithms</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/heap/">heap</a>, <a href="/recipes/tags/heapq/">heapq</a>, <a href="/recipes/tags/pqdict/">pqdict</a>, <a href="/recipes/tags/priority_queue/">priority_queue</a>). Revision 4. </p> <p>An indexed priority queue implemented in pure python as a dict-like class. It is a stripped-down version of <a href="https://pypi.python.org/pypi/pqdict/">pqdict</a>. A Priority Queue Dictionary maps dictionary keys (dkeys) to updatable priority keys (pkeys).</p> <p>The priority queue is implemented as a binary heap, which supports: </p> <ul> <li><p>O(1) access to the top priority element </p></li> <li><p>O(log n) removal of the top priority element </p></li> <li><p>O(log n) insertion of a new element</p></li> </ul> <p>In addition, an internal dictionary or "index" maps dictionary keys to the position of their entry in the heap. This index is maintained as the heap is manipulated. As a result, a PQ-dict also supports: </p> <ul> <li><p>O(1) lookup of an arbitrary element's priority key </p></li> <li><p>O(log n) removal of an arbitrary element </p></li> <li><p>O(log n) updating of an arbitrary element's priority key</p></li> </ul> <p>A data structure like this can be used to drive simulations, schedulers, certain greedy algorithms, merging streams of sorted data, and other applications where priorities may need to be changed frequently.</p> States to Regions (Python) 2016-05-09T22:24:26-07:00Jackson Killianhttp://code.activestate.com/recipes/users/4194060/http://code.activestate.com/recipes/580661-states-to-regions/ <p style="color: grey"> Python recipe 580661 by <a href="/recipes/users/4194060/">Jackson Killian</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/regions/">regions</a>, <a href="/recipes/tags/states/">states</a>). </p> <p>Python dictionary mapping two letter state abbreviations to their respective regions of the country (i.e. Midwest, North East, etc.) N - North East W - West M - Mid West S - South O - Other</p> LRU dictionary (Python) 2016-04-17T01:22:01-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580644-lru-dictionary/ <p style="color: grey"> Python recipe 580644 by <a href="/recipes/users/4193957/">Felix</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/expiration/">expiration</a>, <a href="/recipes/tags/expiring/">expiring</a>, <a href="/recipes/tags/lru/">lru</a>, <a href="/recipes/tags/lru_cache/">lru_cache</a>, <a href="/recipes/tags/python3/">python3</a>). </p> <p>For most cases where you want to store a limited amount of data, functools.partial is sufficient. However, if you want or need more control over your data, especially specifying an expiration date for your entries, this can come in handy.</p> <p>Read the docstrings for implementation details.</p> wrist friendly dictionary (Python) 2013-08-18T22:54:26-07:00Ariel Keselmanhttp://code.activestate.com/recipes/users/4187559/http://code.activestate.com/recipes/578644-wrist-friendly-dictionary/ <p style="color: grey"> Python recipe 578644 by <a href="/recipes/users/4187559/">Ariel Keselman</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/interface/">interface</a>, <a href="/recipes/tags/nested/">nested</a>). </p> <p>this dictionary allows easy manual creation of nested hierarchies, like so:</p> <p>window.style.width=5</p> <p>or... </p> <p>window['background-color'].rgb= 255,255,255</p> Convert Dictionary to XML (Python) 2009-10-28T10:39:44-07:00Adam M Prosthttp://code.activestate.com/recipes/users/4172124/http://code.activestate.com/recipes/576939-convert-dictionary-to-xml/ <p style="color: grey"> Python recipe 576939 by <a href="/recipes/users/4172124/">Adam M Prost</a> (<a href="/recipes/tags/convert/">convert</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/xml/">xml</a>). </p> <p>A simple four line function for converting a dictionary into a simple xml string.</p>