Reputation: 33
I'm trying to count the frequency in which a word is used. If I say "hi im Nick" it will give me one count for each word. I followed the book but when I do something like "i am high as a kite" I get 3 counts for i and a. Is there a way to only count i and a by themselves?
txt = "i am high as a kite"
x = txt.split(" ")
for num_of_instances in x:
count = txt.count(num_of_instances)
print(num_of_instances, count)
Upvotes: 3
Views: 42
Reputation: 21991
May I recommend using the collections
module that comes with Python's standard library?
>>> import collections
>>> text = 'i am high as a kite'
>>> word_count = collections.Counter(text.split())
>>> word_count
Counter({'i': 1, 'am': 1, 'high': 1, 'as': 1, 'a': 1, 'kite': 1})
>>> character_count = collections.Counter(text)
>>> character_count
Counter({' ': 5, 'i': 3, 'a': 3, 'h': 2, 'm': 1, 'g': 1, 's': 1, 'k': 1, 't': 1, 'e': 1})
>>>
It has a class in it called Counter
that is built entirely for the purpose of counting things for you. Its interface is somewhat similar to the language's built-in dict
type. You can find its documentation using this link.
Upvotes: 1
Reputation: 22766
Just do:
x.count(num_of_instances)
instead of:
txt.count(num_of_instances)
still, this will repeat counting repeating words in sentences like "to be or not to be"
(be
and to
will be counted twice), better use a set to remove these duplicates (however you will lose the order in which the words appear):
txt = "to be or not to be"
x = txt.split(" ")
for num_of_instances in set(x):
count = x.count(num_of_instances)
print(num_of_instances, count)
Output (the order may change each time you execute the code):
be 2
to 2
not 1
or 1
It's better if you use a Counter
object:
from collections import Counter
txt = "to be or not to be"
x = Counter(txt.split(" "))
for word, count in x.items():
print(word, count)
Output:
to 2
be 2
or 1
not 1
Upvotes: 4