Advertisement
Guest User

Untitled

a guest
Apr 24th, 2019
90
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.60 KB | None | 0 0
  1. # Count the number of occurrences of each of the words in a sentence
  2.  
  3. input_string = input ( "Enter your string - ")
  4.  
  5. # Split the string into words
  6. words = input_string.split(" ")
  7.  
  8. # Initialize a word counter ( as a dictionary)
  9. word_count = { }
  10.  
  11.  
  12. # # Iterate through the words and start updating counts
  13. for word in words :
  14.  
  15. # If a word is not already counted, add it to the counter
  16. if word_count.get(word) == None :
  17. word_count[word] = 1
  18. # If already present, increment the word count
  19. else :
  20. word_count[word] = word_count[word] + 1
  21.  
  22. # Finally, print the counter
  23. print ( word_count)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement