This is what I have to count each word in a document:
from collections import defaultdict
word_dict=defaultdict(int)
def count_words(newstring):
words=newstring.lower().split()
for word in words:
word_dict[word]+=1
When I print word_dict, I got the following results:
defaultdict(<type 'int'>, {'rate': 1, 'babo-free': 1, 'risk': 3, 'interest': 1})
I need to add each count so that total_count variable should be equal to 6.
I guess this may be too easy for many of you guys, but as a beginner, I don’t know where to start.
Using
iteritemsyou can get a list of key value pairs. With that you can use a for loop to sum the numbers together.Eg