python单词计数
统计单词数量
统计单词数量,拆分为以下几步:
- 需要剔除文本中的标点符号与换行符
- 大小写转换
- 单词拆分计数
详细代码如下:
import re
def wordcount(text):
"""
单词计数
标点去除
大小写转换
"""
count = {}
# 替换标点
text = text.replace(",","").replace(".","").replace("\n","")
# 转换大小写,分割
text = text.lower().split(" ")
for word in text:
if word in count.keys():
count[word] += 1
else:
count[word] = 1
return count
text = """
Got this panda plush toy for my daughter's birthday,
who loves it and takes it everywhere. It's soft and
super cute, and its face has a friendly look. It's
a bit small for what I paid though. I think there
might be other options that are bigger for the
same price. It arrived a day earlier than expected,
so I got to play with it myself before I gave it
to her.
"""
wc = wordcount(text)
使用debug模式查看代码逻辑,配置debug的参数:
以debug模式运行,运行正常。