<br><br><div><span class="gmail_quote">On 4/7/07, <b class="gmail_sendername">Steven Bethard</b> &lt;<a href="mailto:steven.bethard@gmail.com">steven.bethard@gmail.com</a>&gt; wrote:</span><blockquote class="gmail_quote" style="border-left: 1px solid rgb(204, 204, 204); margin: 0pt 0pt 0pt 0.8ex; padding-left: 1ex;">
On 4/7/07, Steven Bethard &lt;<a href="mailto:steven.bethard@gmail.com">steven.bethard@gmail.com</a>&gt; wrote:<br>&gt; Here&#39;s a patch implementing collections.counts() as suggested above:<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<a href="http://bugs.python.org/1696199">
http://bugs.python.org/1696199</a><br>&gt;<br>&gt; Example usage, from the docstring::<br>&gt;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt; items = &#39;acabbacba&#39;<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt; item_counts = counts(items)<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&gt;&gt;&gt; for item in &#39;abcd&#39;:
<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...&nbsp;&nbsp;&nbsp;&nbsp; print item, item_counts[item]<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;...<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;a 4<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;b 3<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;c 2<br>&gt;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;d 0<br><br>Guido commented in the tracker that it would be worth discussing<br>whether that last item (``item_counts[&#39;d&#39;]``) should return 0 (as a
<br>defaultdict would) or raise KeyError (as a dict would).<br><br>Anyone have a good motivation for one approach or the other?</blockquote><div><br><br>I say 0 if it is really meant to represent a count as the absense of something is 0.&nbsp; The object returned is being viewed as an object representing the count of items, not specifically as a dict.
<br><br>-Brett</div></div>