Most viewed recipes tagged "dict"http://code.activestate.com/recipes/tags/dict/views/2017-01-17T20:05:10-08:00ActiveState Code RecipesPython Dictionary of US States and Territories (Python) 2010-07-14T19:37:21-07:00Mike Shultzhttp://code.activestate.com/recipes/users/4174394/http://code.activestate.com/recipes/577305-python-dictionary-of-us-states-and-territories/ <p style="color: grey"> Python recipe 577305 by <a href="/recipes/users/4174394/">Mike Shultz</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/states/">states</a>, <a href="/recipes/tags/territories/">territories</a>). </p> <p>Handy for matching state abbreviations with their names. Also for iteration.</p> A Simple Namespace Class (Python) 2011-10-03T21:12:41-07:00Eric Snowhttp://code.activestate.com/recipes/users/4177816/http://code.activestate.com/recipes/577887-a-simple-namespace-class/ <p style="color: grey"> Python recipe 577887 by <a href="/recipes/users/4177816/">Eric Snow</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/namespaces/">namespaces</a>). Revision 3. </p> <p>"Namespaces are one honking great idea -- let's do more of those!" -- <a href="http://www.python.org/dev/peps/pep-0020/">The Zen of Python</a></p> <p>For when you want a simple, easy namespace, but you don't want it cluttered up with Python's object machinery.</p> Flipdict -- python dict that also maintains a one-to-one inverse mapping (Python) 2009-12-03T14:43:52-08:00Francis Carrhttp://code.activestate.com/recipes/users/4172444/http://code.activestate.com/recipes/576968-flipdict-python-dict-that-also-maintains-a-one-to-/ <p style="color: grey"> Python recipe 576968 by <a href="/recipes/users/4172444/">Francis Carr</a> (<a href="/recipes/tags/1_to_1/">1_to_1</a>, <a href="/recipes/tags/bijection/">bijection</a>, <a href="/recipes/tags/bijective/">bijective</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/injection/">injection</a>, <a href="/recipes/tags/injective/">injective</a>, <a href="/recipes/tags/inverse/">inverse</a>, <a href="/recipes/tags/invert/">invert</a>, <a href="/recipes/tags/mapping/">mapping</a>, <a href="/recipes/tags/one_to_one/">one_to_one</a>). Revision 6. </p> <p>A Flipdict is a python dict subclass that maintains a one-to-one inverse mapping. Each key maps to a unique value, and each value maps back to that same key. Each instance has a "flip" attribute to access the inverse mapping.</p> 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> AttrDict (Python) 2009-11-28T05:39:08-08:00Chris Joneshttp://code.activestate.com/recipes/users/4171447/http://code.activestate.com/recipes/576972-attrdict/ <p style="color: grey"> Python recipe 576972 by <a href="/recipes/users/4171447/">Chris Jones</a> (<a href="/recipes/tags/attr/">attr</a>, <a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>). </p> <p>Dictionary object that can also be accessed via attributes</p> Classifying letters as vowels or consonants and counting their frequencies (Python) 2017-01-17T20:05:10-08:00Vasudev Ramhttp://code.activestate.com/recipes/users/4173351/http://code.activestate.com/recipes/580749-classifying-letters-as-vowels-or-consonants-and-co/ <p style="color: grey"> Python recipe 580749 by <a href="/recipes/users/4173351/">Vasudev Ram</a> (<a href="/recipes/tags/assertions/">assertions</a>, <a href="/recipes/tags/comprehension/">comprehension</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionaries/">dictionaries</a>, <a href="/recipes/tags/dict_comp/">dict_comp</a>, <a href="/recipes/tags/python/">python</a>, <a href="/recipes/tags/python2/">python2</a>, <a href="/recipes/tags/tuple/">tuple</a>, <a href="/recipes/tags/unpack/">unpack</a>). </p> <p>This recipe shows how to take a string as input and classify the characters in it as vowels, consonants or neither. The frequency of each vowel is calculated and the frequency of all the consonants in total is calculated. The program logic is fairly simple, and uses a dictionary comprehension and a dict; the more interesting thing about it, is that it illustrates 8 Python language features in under 35 lines of code.</p> <p>More details and sample output here:</p> <p><a href="https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html" rel="nofollow">https://jugad2.blogspot.in/2017/01/classifying-letters-and-counting-their.html</a></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> 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> Convert XML to Dictionary (Python) 2009-10-28T10:42:17-07:00Adam M Prosthttp://code.activestate.com/recipes/users/4172124/http://code.activestate.com/recipes/576940-convert-xml-to-dictionary/ <p style="color: grey"> Python recipe 576940 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/json/">json</a>, <a href="/recipes/tags/simple/">simple</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 2. </p> <p>A simple function to convert a headerless XML string into a dictionary using only simplejson and re.</p> Reading XML into dict-like object (Python) 2013-03-14T18:50:07-07:00Lucas Oliveirahttp://code.activestate.com/recipes/users/4185629/http://code.activestate.com/recipes/578492-reading-xml-into-dict-like-object/ <p style="color: grey"> Python recipe 578492 by <a href="/recipes/users/4185629/">Lucas Oliveira</a> (<a href="/recipes/tags/attributes/">attributes</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/xml/">xml</a>). Revision 4. </p> <ul> <li>Load XML, tree or root</li> <li>Make its children available through __getitem__</li> <li>Make its attributes available through __getattr__</li> <li>If child is requested, return an instance created with child as new root</li> <li>Make its text accessible through __getattr__, using attribute "text"</li> </ul> Unrestricted Grammar used like a dictionary (Python) 2009-01-04T19:10:28-08:00Shea Kauffmanhttp://code.activestate.com/recipes/users/4168682/http://code.activestate.com/recipes/576606-unrestricted-grammar-used-like-a-dictionary/ <p style="color: grey"> Python recipe 576606 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>). </p> <p>Programs can be written as substitutions. This allows a set of substitution rules to be defined and then strings are transformed using these rules in a left recursive manner.</p> Python Dictionary of US States and Territories (Python) 2014-10-11T19:56:18-07:00Jeff vGhttp://code.activestate.com/recipes/users/4190943/http://code.activestate.com/recipes/578949-python-dictionary-of-us-states-and-territories/ <p style="color: grey"> Python recipe 578949 by <a href="/recipes/users/4190943/">Jeff vG</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/states/">states</a>, <a href="/recipes/tags/territories/">territories</a>). </p> <p>Handy for matching state abbreviations with their names. Also for iteration.</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> Dictionary Who's Keys Act Like Attributes As Well (Python) 2011-05-26T20:15:16-07:00Sunjay Varmahttp://code.activestate.com/recipes/users/4174115/http://code.activestate.com/recipes/577590-dictionary-whos-keys-act-like-attributes-as-well/ <p style="color: grey"> Python recipe 577590 by <a href="/recipes/users/4174115/">Sunjay Varma</a> (<a href="/recipes/tags/attribute/">attribute</a>, <a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/keys/">keys</a>). </p> <p>Think of this as a JavaScript object. In JavaScript, the objects can be referenced by indexing (e.g. d[name]) or by directly using the dot (.) operator (e.g. d.name).</p> <p>This is the same concept. </p> <p><strong>Note to Python 2.4 Users:</strong> You will need to change the "except KeyError as e:" line to "except KeyError, (e):".</p> 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> setdict (Python) 2010-11-27T03:00:28-08:00thom nealehttp://code.activestate.com/recipes/users/4176069/http://code.activestate.com/recipes/577471-setdict/ <p style="color: grey"> Python recipe 577471 by <a href="/recipes/users/4176069/">thom neale</a> (<a href="/recipes/tags/dict/">dict</a>, <a href="/recipes/tags/dictionary/">dictionary</a>, <a href="/recipes/tags/set/">set</a>). </p> <p>Set-like operations for dictionaries</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> 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>