Dictionary Comprehension
collections.Counter
from collections import Counter
text = "Hello, this is a sentence to show how this works."
char_frequency = Counter(text)
print(char_frequency)
>>> Counter({'e': 6, ' ': 6, 'h': 3, 'c': 3, 'r': 3, 'd': 3, 'a': 2, 't': 2, 'l': 2, 'b': 2, 'T': 1, 'm': 1, 'y': 1, 'o': 1, 's': 1, 'v': 1, 'i': 1, 'p': 1, '.': 1})
References
- More on the
collections.Counter
: Python Morsels article, 5 min read on the subject