A word count problem is one of
the basic program that we come across in python.Being a programmer ,
we should be able to understand such kind of program and able to
execute it.
Step
1 : Create the dataset , here it is done using the list.
words =
['A','boy','is','a','the','boy','is','the','the','india']
Step
2: Create an empty dictionary
wordcount = {}
Step
3: Traverse the loop and check for the word in list whether it is
available in wordcount dictionary
for word in words:
if word not in wordcount:
wordcount[word] =1
else:
wordcount[word] +=1
Step 4 : Print the value
using key,value pair
for k,v in wordcount.items():
print(k,v)
Complete
code snippet
words =
['A','boy','is','a','the','boy','is','the','the','india']
wordcount = {}
for word in words:
if word not in wordcount:
wordcount[word] =1
else:
wordcount[word] +=1
for k,v in wordcount.items():
print(k,v)
Output
:
you can find these codes in my
github repository :- https://github.com/sangam92/python_tutorials
No comments:
Post a Comment