function for counting items in a sequence

Discussion in 'Python' started by Steven Bethard, Apr 5, 2007.

  1. 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.

    STeVe
    Steven Bethard, Apr 5, 2007
    #1
    1. Advertising

  2. Steven Bethard <> 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
    Alex Martelli, Apr 5, 2007
    #2
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Rob Meade

    Counting items in an array?

    Rob Meade, Nov 25, 2003, in forum: ASP .Net
    Replies:
    4
    Views:
    8,252
    Rob Meade
    Nov 25, 2003
  2. Andrew Banks

    Counting items and returning values

    Andrew Banks, Apr 1, 2004, in forum: ASP .Net
    Replies:
    3
    Views:
    334
    Cliff Harris
    Apr 1, 2004
  3. Replies:
    1
    Views:
    739
    Damien
    May 24, 2005
  4. stef mientki
    Replies:
    13
    Views:
    633
    stef mientki
    Oct 20, 2007
  5. edwardfredriks

    counting up instead of counting down

    edwardfredriks, Sep 6, 2005, in forum: Javascript
    Replies:
    6
    Views:
    197
    Dr John Stockton
    Sep 7, 2005
Loading...

Share This Page