[Python-Dev] function for counting items in a sequence (original) (raw)

Brett Cannon brett at python.org
Sun Apr 8 03:31:31 CEST 2007


On 4/7/07, Steven Bethard <steven.bethard at gmail.com> wrote:

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' > >>> itemcounts = counts(items) > >>> for item in 'abcd': > ... print item, itemcounts[item] > ... > a 4 > b 3 > c 2 > d 0 Guido commented in the tracker that it would be worth discussing whether that last item (itemcounts['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?

I say 0 if it is really meant to represent a count as the absense of something is 0. The object returned is being viewed as an object representing the count of items, not specifically as a dict.

-Brett -------------- next part -------------- An HTML attachment was scrubbed... URL: http://mail.python.org/pipermail/python-dev/attachments/20070407/a14cfdf9/attachment-0001.html



More information about the Python-Dev mailing list