Python - Is a dictionary slow to find frequency of each character?

Posted by psihodelia on Stack Overflow See other posts from Stack Overflow or by psihodelia
Published on 2010-03-26T09:31:23Z Indexed on 2010/03/26 9:33 UTC
Read the original article Hit count: 148

I am trying to find a frequency of each symbol in any given text using an algorithm of O(n) complexity. My algorithm looks like:

s = len(text) 
P = 1.0/s 
freqs = {} 
for char in text: 
    try: 
       freqs[char]+=P 
    except: 
       freqs[char]=P 

but I doubt that this dictionary-method is fast enough, because it depends on the underlying implementation of the dictionary methods. Is this the fastest method?

© Stack Overflow or respective owner

Related posts about python

Related posts about algorithm