import string
sentence = raw_input("Enter sentence:")
for i in string.punctuation:
sentence = sentence.replace(i," ")
word_list = sentence.split()
word_list.sort(key=str.lower)
print word_list
for j in word_list:
print j,":",word_list.count(j)
word_list.remove(j)
When I use this code and type in a sample sentence, some of my words are not counted correctly:
Sample sentence: I, are.politics:wodng!"frail A P, Python. Python Python frail
output:
['A', 'are', 'frail', 'frail', 'I', 'P', 'politics', 'Python', 'Python', 'Python', 'wodng']
A : 1 frail : 2 I : 1 politics : 1 Python : 3 wodng : 1
What happened to the words "are" and "P"? I know the problem is happening in the last few lines but I don't know what's causing it.
Thanks!