function for counting items in a sequence

Alex Martelli aleax at mac.com
Thu Apr 5 06:37:55 CEST 2007


Steven Bethard <steven.bethard at gmail.com> wrote:

> Alex Martelli wrote:
>  > If we had a "turn sequence into bag" function somewhere
>  > (and it might be worth having it for other reasons):
>  >
>  > def bagit(seq):
>  >   import collections
>  >   d = collections.defaultdict(int)
>  >   for x in seq: d[x] += 1
>  >   return d
> 
> I use this function all the time -- I call it dicttools.count().  I'd
> love to see this appear in the collections module or somewhere else in
> the stdlib.

Maybe you should propose it in python-dev -- it does seem a reasonable
utility addition to the collections module, after all.


Alex



More information about the Python-list mailing list