Possible Duplicate:
how to get the number of occurrences of each character using python
What is the best way to obtain the count of each character in a string and store it(I'm using a dictionary for this - can this choice make a big difference?)? A couple of ways that I thought of:
1.
for character in string:
if character in characterCountsDict:
characterCountsDict[character] += 1
else:
characterCountsDict[character] = 1
2.
character = 0
while character < 127:
characterCountsDict[str(unichr(character))] = string.count(str(unichr(character))
character += 1
I think the second method is better... But is either of them good? Is there a much better way to do this?