[Python-Dev] function for counting items in a sequence

Steven Bethard steven.bethard at gmail.com
Sun Apr 8 03:01:10 CEST 2007


On 4/7/07, Steven Bethard <steven.bethard at gmail.com> wrote:
> Here's a patch implementing collections.counts() as suggested above:
>
>      http://bugs.python.org/1696199
>
> Example usage, from the docstring::
>
>      >>> items = 'acabbacba'
>      >>> item_counts = counts(items)
>      >>> for item in 'abcd':
>      ...     print item, item_counts[item]
>      ...
>      a 4
>      b 3
>      c 2
>      d 0

Guido commented in the tracker that it would be worth discussing
whether that last item (``item_counts['d']``) should return 0 (as a
defaultdict would) or raise KeyError (as a dict would).

Anyone have a good motivation for one approach or the other?

STeVe
-- 
I'm not *in*-sane. Indeed, I am so far *out* of sane that you appear a
tiny blip on the distant coast of sanity.
        --- Bucky Katt, Get Fuzzy


More information about the Python-Dev mailing list