letter frequency counter / your thoughts..

Ian Kelly ian.g.kelly at gmail.com
Wed May 7 14:31:43 EDT 2008


On Wed, May 7, 2008 at 11:30 AM, Paul Melis <paul at floorball-flamingos.nl> wrote:
>     dic = {}
>     for letter in strng:
>         if letter not in dic:
>             dic[letter] = 0
>         dic[letter] += 1

As a further refinement, you could use the defaultdict class from the
collections module:

   dic = defaultdict(int)
       for letter in strng:
           dic[letter] += 1



More information about the Python-list mailing list