Popular recipes tagged "dictionary" but not "class"http://code.activestate.com/recipes/tags/dictionary-class/2016-05-09T22:24:26-07:00ActiveState Code RecipesStates 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> LRU dictionary (Python) 2016-04-17T01:22:51-07:00Felixhttp://code.activestate.com/recipes/users/4193957/http://code.activestate.com/recipes/580645-lru-dictionary/ <p style="color: grey"> Python recipe 580645 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/expires/">expires</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/python/">python</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> 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> 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> 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> 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> 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> Ordered 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> 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> Dynamically create a dictionary of ascending paths (Python) 2012-12-03T22:50:47-08:00Scott S-Allenhttp://code.activestate.com/recipes/users/4181178/http://code.activestate.com/recipes/578354-dynamically-create-a-dictionary-of-ascending-paths/ <p style="color: grey"> Python recipe 578354 by <a href="/recipes/users/4181178/">Scott S-Allen</a> (<a href="/recipes/tags/bootstrap/">bootstrap</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/directory/">directory</a>, <a href="/recipes/tags/dynamic/">dynamic</a>, <a href="/recipes/tags/flexible/">flexible</a>, <a href="/recipes/tags/path/">path</a>, <a href="/recipes/tags/portable/">portable</a>). </p> <p>Ripped from a more elaborate bootstrap script. A sequential set of parent-directories were required for environment variables and subsequent, portable. auto-discovery. </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> 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> Useful Unrestricted Grammar (Python) 2012-03-27T19:30:47-07:00Shea Kauffmanhttp://code.activestate.com/recipes/users/4168682/http://code.activestate.com/recipes/578087-useful-unrestricted-grammar/ <p style="color: grey"> Python recipe 578087 by <a href="/recipes/users/4168682/">Shea Kauffman</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/grammar/">grammar</a>, <a href="/recipes/tags/grammer/">grammer</a>, <a href="/recipes/tags/language/">language</a>, <a href="/recipes/tags/thue/">thue</a>, <a href="/recipes/tags/turing_machine/">turing_machine</a>, <a href="/recipes/tags/unrestricted/">unrestricted</a>). </p> <p>This is an implementation of an easy to use unrestricted grammar. It could be used for quickly prototyping a DSL, testing a post-formal system, or parsing test in a way that requires context. It would however be extremely slow for simple substitutions.</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> Sorting a dict's items and keys (Python) 2012-02-04T04:23:01-08:00George V. Reillyhttp://code.activestate.com/recipes/users/4161272/http://code.activestate.com/recipes/578031-sorting-a-dicts-items-and-keys/ <p style="color: grey"> Python recipe 578031 by <a href="/recipes/users/4161272/">George V. Reilly</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/sorting/">sorting</a>). </p> <p><a href="https://github.com/collective/icalendar">icalendar</a> uses its own CaselessDict as the base of many classes. I needed to produce the keys and items in a canonical order, so that certain keys would appear first.</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> Yet Another Ordered Dictionary (Python) 2011-08-07T12:14:56-07:00Lucio Santihttp://code.activestate.com/recipes/users/4178886/http://code.activestate.com/recipes/577826-yet-another-ordered-dictionary/ <p style="color: grey"> Python recipe 577826 by <a href="/recipes/users/4178886/">Lucio Santi</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/linked_list/">linked_list</a>, <a href="/recipes/tags/ordered/">ordered</a>). </p> <p>An implementation of dictionaries preserving key insertion order. Despite using a doubly-linked list to keep track of the appropriate order, this list is actually embedded in the dictionary. As a consequence, there is little space penalty, and also every operation exhibits an efficient implementation (i.e., no need to perform lookups or deletions multiple times, as it happens with other versions of this data structure).</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> dict2xml (Python) 2011-07-19T12:32:57-07:00nuggetierhttp://code.activestate.com/recipes/users/4178225/http://code.activestate.com/recipes/577739-dict2xml/ <p style="color: grey"> Python recipe 577739 by <a href="/recipes/users/4178225/">nuggetier</a> (<a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <p>convert dictionary / list structures into xml structure</p>