Skip to content

Instantly share code, notes, and snippets.

@diallobakary4
Created June 20, 2016 11:29
Show Gist options
  • Save diallobakary4/332660ae3d4c8bd0c882bbbad8c9b32f to your computer and use it in GitHub Desktop.
Save diallobakary4/332660ae3d4c8bd0c882bbbad8c9b32f to your computer and use it in GitHub Desktop.
Count the number of each word in a text
# scrip that print the number of time a word appear in a text
#import the text file and read it
test = open("test list of word r.txt", 'r');
text = test.read();
#build a list of the words in the text
listOfWord = text.split()
# dictionnay that will contain the words
# and the number of times they appear in the string
dico = {};
#dico building
for words in listOfWord :
dico [words]= text.count(words)
# printing the words and the numbers of time they appear
for word in dico :
print(word),
print (dico[word])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment