Advertisement
Guest User

Untitled

a guest
Aug 20th, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 0.87 KB | None | 0 0
  1. def strip_word(word):
  2. # Create alphabet list of lowercase + uppercase letters
  3. alphabet = []
  4. for letter in list(range(97,123)) + list(range(65, 91)):
  5. alphabet.append(chr(letter))
  6.  
  7. alphabet.append("'")
  8.  
  9. indices = []
  10. for i, ch in enumerate(word):
  11. if ch not in alphabet:
  12. indices.append(i)
  13.  
  14. w = word
  15. for i in reversed(indices):
  16. i = int(i)
  17. w = w[:i] + w[i+1:]
  18.  
  19. return w
  20.  
  21. def count_words(string):
  22. word_dict = {}
  23. word_list = string.lower().split()
  24. tmp_list = []
  25. for word in word_list:
  26. tmp_list.append(strip_word(word))
  27. word_list = tmp_list
  28. del tmp_list
  29.  
  30. word_set = set(word_list)
  31. for word in word_set:
  32. word_dict[word] = 0
  33.  
  34. for word in word_list:
  35. word_dict[word] += 1
  36.  
  37. return word_dict
  38.  
  39. print(count_words("Oh what a day what a lovely day!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement