word = 'adsgsdfhsdgfhsdfggsfdgsdfgs'
Well, if there is a character string like this, I want to count the frequency of appearance of characters, but I do not want to do anything complicated, so I thought about how to do it.
#Make a set of characters from a string
char_set = { x for x in word }
#Represent the frequency of appearance in a dictionary
char_freq = { x : word.count(x) for x in char_set }
I think that nltk is necessary to calculate the frequency of appearance of character strings in a more serious manner, but even with this, I felt that it would fall into my mind. Good.
It seems that the Counter object added in Python 2.7 is just right. yasuharu519 Thank you for your information.
from collections import Counter
c = Counter(word)
Recommended Posts