<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DZone Snippets: dict code</title>
    <link>http://snippets.dzone.com/posts</link>
    <pubDate>Sat, 26 Jul 2008 22:22:10 GMT</pubDate>
    <description>DZone Snippets: dict code</description>
    <item>
      <title>Collection of a bunch of named stuff</title>
      <link>http://snippets.dzone.com/posts/show/1725</link>
      <description>Taken from this &lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/52308&gt;recipe&lt;/a&gt; and its comments.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;class bunch(dict):&lt;br /&gt;    def __init__(self,**kw):&lt;br /&gt;        dict.__init__(self,kw)&lt;br /&gt;        self.__dict__.update(kw)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;Usage is simple.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; o = bunch(a='A', b='B')&lt;br /&gt;&gt;&gt;&gt; o&lt;br /&gt;{'a': 'A', 'b': 'B'}&lt;br /&gt;&gt;&gt;&gt; o.a&lt;br /&gt;'A'&lt;br /&gt;&gt;&gt;&gt; o.b&lt;br /&gt;'B'&lt;br /&gt;&gt;&gt;&gt; &lt;br /&gt;&lt;/code&gt;&lt;br /&gt;You can use it as both an object and dict.</description>
      <pubDate>Wed, 22 Mar 2006 13:14:51 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1725</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>An interval mapping class</title>
      <link>http://snippets.dzone.com/posts/show/1492</link>
      <description>A kind of dictionary which allows you to map data intervals to values.&lt;br /&gt;Download 'intervalmap' from this &lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/457411&gt;recipe page&lt;/a&gt;.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt;&gt; i = intervalmap()&lt;br /&gt;&gt;&gt;&gt; i[0:5] = '0-5'&lt;br /&gt;&gt;&gt;&gt; i[8:12] = '8-12'&lt;br /&gt;&gt;&gt;&gt; print i[2]&lt;br /&gt;0-5&lt;br /&gt;&gt;&gt;&gt; print i[10]&lt;br /&gt;8-12&lt;br /&gt;&gt;&gt;&gt;&lt;br /&gt;&lt;/code&gt;</description>
      <pubDate>Wed, 15 Feb 2006 21:11:21 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/1492</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Ordered dictionary</title>
      <link>http://snippets.dzone.com/posts/show/919</link>
      <description>This is part of the &lt;a href=http://www.voidspace.org.uk/python/pythonutils.html&gt;pythonutils&lt;/a&gt; package.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;od = OrderedDict()&lt;br /&gt;od['key1'] = 'value 1'&lt;br /&gt;od['key2'] = 'value 2'&lt;br /&gt;print od&lt;br /&gt;{'key1': 'value 1', 'key2': 'value 2'}&lt;br /&gt;&lt;br /&gt;print od.sequence&lt;br /&gt;['key1', 'key2']&lt;br /&gt;od.sequence.reverse()&lt;br /&gt;print od&lt;br /&gt;{'key2': 'value 2', 'key1': 'value 1'}&lt;br /&gt;print od.sequence&lt;br /&gt;['key2', 'key1']&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;This dict keeps its insertion order and you can do other&lt;br /&gt;manipulations. See more details &lt;a href=http://www.voidspace.org.uk/python/odict.html&gt;here&lt;/a&gt;</description>
      <pubDate>Tue, 29 Nov 2005 12:42:29 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/919</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
    <item>
      <title>Default dictionary</title>
      <link>http://snippets.dzone.com/posts/show/715</link>
      <description>I always want to do this, like I did in Perl.&lt;br /&gt;&lt;code&gt;&lt;br /&gt;&gt;&gt; d = DefaultDict(0)&lt;br /&gt;&gt;&gt; d[key] += 1     # no need to use d.get or d.setdefault&lt;br /&gt;&lt;br /&gt;&gt;&gt; d = DefaultDict([])  # similarly with list value&lt;br /&gt;&gt;&gt; d[key].append(item)&lt;br /&gt;&lt;/code&gt;&lt;br /&gt;See the implementation by Peter Norvig (of AI &amp; Google fame) &lt;br /&gt;&lt;a href=http://aspn.activestate.com/ASPN/Cookbook/Python/Recipe/389639&gt;here&lt;/a&gt;</description>
      <pubDate>Thu, 15 Sep 2005 01:11:37 GMT</pubDate>
      <guid>http://snippets.dzone.com/posts/show/715</guid>
      <author>korakot (Korakot Chaovavanich)</author>
    </item>
  </channel>
</rss>
