list.count() with no arguments

Discussion in 'Python' started by Johan Hahn, Mar 27, 2005.

  1. Johan Hahn

    Johan Hahn Guest

    Wouldn't it be nice if list.count, called without any arguments,
    returned a dict with the list's unique items as keys and their
    frequency of occurance as values?

    >>> [1,2,1,'a'].count()

    {'a': 1, 1: 2, 2: 1}
    >>> 'hello world'.count()

    {' ': 1, 'e': 1, 'd': 1, 'h': 1, 'l': 3, 'o': 2, 'r': 1, 'w': 1}

    ....johahn
    Johan Hahn, Mar 27, 2005
    #1
    1. Advertising

  2. Johan Hahn wrote:
    > Wouldn't it be nice if list.count, called without any arguments,
    > returned a dict with the list's unique items as keys and their
    > frequency of occurance as values?


    No. It would require all sequences to support this protocol, which
    would be tedious to implement. Some day, we may have a bag type,
    so it would be better if this type supported frequency counting.

    Regards,
    Martin
    =?ISO-8859-1?Q?=22Martin_v=2E_L=F6wis=22?=, Mar 27, 2005
    #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. Replies:
    3
    Views:
    913
    Natty Gur
    Nov 3, 2003
  2. lonelyplanet999

    Program can't count arguments

    lonelyplanet999, Oct 28, 2003, in forum: Java
    Replies:
    12
    Views:
    633
    lonelyplanet999
    Nov 2, 2003
  3. Edward Diener
    Replies:
    14
    Views:
    4,941
    Josiah Carlson
    Apr 6, 2004
  4. Neo
    Replies:
    10
    Views:
    658
    sushant
    Jan 20, 2005
  5. efelnavarro09
    Replies:
    2
    Views:
    934
    efelnavarro09
    Jan 26, 2011
Loading...

Share This Page