|
马上注册,结交更多好友,享用更多功能^_^
您需要 登录 才可以下载或查看,没有账号?立即注册
x
# 文本词频统计2
# threekingdomsV1:找一个三国演义的文件,保存为threekingdoms.txt,放在同一目录里面
# 记录输出出现次数最多的前十五个人物名输出
- import jieba
- filename = "threekingdoms.txt"
- txt = open(filename, "r", encoding="utf-8").read()
- words = jieba.lcut(txt)
- counts = {}
- for word in words:
- if len(word)==1:
- continue
- else:
- counts[word] = counts.get(word, 0) + 1
- items = list(counts.items())
- items.sort(key=lambda x:x[1],reverse=True)
- # 将出现次数最多的前十五个人物名输出
- for i in range(15):
- word, count = items[i]
- print("{0:<10}{1:>5}".format(word, count))
复制代码 |
|