Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- def strip_word(word):
- # Create alphabet list of lowercase + uppercase letters
- alphabet = []
- for letter in list(range(97,123)) + list(range(65, 91)):
- alphabet.append(chr(letter))
- alphabet.append("'")
- indices = []
- for i, ch in enumerate(word):
- if ch not in alphabet:
- indices.append(i)
- w = word
- for i in reversed(indices):
- i = int(i)
- w = w[:i] + w[i+1:]
- return w
- def count_words(string):
- word_dict = {}
- word_list = string.lower().split()
- tmp_list = []
- for word in word_list:
- tmp_list.append(strip_word(word))
- word_list = tmp_list
- del tmp_list
- word_set = set(word_list)
- for word in word_set:
- word_dict[word] = 0
- for word in word_list:
- word_dict[word] += 1
- return word_dict
- print(count_words("Oh what a day what a lovely day!"))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement