大家好,我是 python 新手,需要编写一个程序来消除标点符号,然后计算字符串中的单词数。所以我有这个:
import sys
import string
def removepun(txt):
for punct in string.punctuation:
txt = txt.replace(punct,"")
print txt
mywords = {}
for i in range(len(txt)):
item = txt[i]
count = txt.count(item)
mywords[item] = count
return sorted(mywords.items(), key = lambda item: item[1], reverse=True)
问题是它返回字母并计算它们,而不是我希望的单词。你能帮我解决这个问题吗?