Python实现wordcount
import string
def wordcount(text):
# 去掉标点符号
translator = str.maketrans('', '', string.punctuation)
text = text.translate(translator)
# 把字符串转换成小写
text = text.lower()
# 分割字符串成单词列表
words = text.split()
# 统计每个单词出现的次数
word_counts = {}
for word in words:
if word in word_counts:
word_counts[word] += 1
else:
word_counts[word] = 1
return word_counts