资源简介
用python分别读取了哈姆雷特和三国演义的txt文本文件,并结合了python第三方库jieba对三国演义中的人物出场进行了排序。

代码片段和文件信息
# -*- coding: utf-8 -*-
“““
Created on Tue Sep 4 15:48:05 2018
@author: weilangao
“““
#def getText():
# txt=open(“hamlet.txt““r“).read()
# txt=txt.lower()
# for ch in ‘!@#$%^&*():“;<>.?/{[]}\+-=~|‘:#去掉特殊字符
# txt=txt.replace(ch““)
# return txt
#
#hamletTxt=getText()
#words=hamletTxt.split()
#counts={}
#for word in words:
# counts[word]=counts.get(word0)+1
#items=list(counts.items())
#items.sort(key=lambda x:x[1]reverse=True)#按照列表的键值对的第二个值进行排序
#for i in range(10):
# wordcount=items[i]
# print(“{0:<10}{1:>5}“.format(wordcount))
import jieba
txt=open(“threekingdoms.txt““r“encoding=“utf-8“).read()
excludes={“将军““却说““荆州““二人““不可““不能““如此““如何““主公““军士““左右“}
words=jieba.lcut(txt)
counts={}
for word in words:
if len(word)==1:
continue
elif word==“诸葛亮“ or word==“孔明曰“:
rword=“孔明“
elif word==“关公“ or word==“云长“:
rword=“关羽“
elif word==“玄德“ or word==“玄德曰“:
rword=“刘备“
elif word==“孟德“ or word==“丞相“:
rword=“曹操“
else:
rword=word
counts[rword]=counts.get(rword0)+1
for word in excludes:
del counts[word]
items=list(counts.items())
items.sort(key=lambda x:x[1]reverse=True)
for i in range(15):
wordcount=items[i]
print(“{0:<10}{1:>5}“.format(wordcount))
属性 大小 日期 时间 名称
----------- --------- ---------- ----- ----
文件 1483 2018-09-04 16:39 5.py
文件 180768 2017-03-17 08:38 hamlet.txt
文件 1767830 2017-03-17 08:38 threekingdoms.txt
- 上一篇:西电软院算法上机1代码
- 下一篇:人工鱼群算法求函数的最大值
相关资源
- Python-BDD100K大规模多样化驾驶视频数据
- Instant Pygame for Python Game Development How
- Biopython Tutorial
- Think Python 2nd
- 一个小小的表白程序(python)
- Python课堂笔记(高淇400集第一季)
- 二级考试python试题12套(包括选择题和
- pywin32_python3.6_64位
- python+ selenium教程
- PycURL(Windows7/Win32)Python2.7安装包 P
- 英文原版-Scientific Computing with Python
- 7.图像风格迁移 基于深度学习 pyt
- 基于Python的学生管理系统
- A Byte of Python(简明Python教程)(第
- Python实例174946
- Python 人脸识别
- Python 人事管理系统
- 基于python-flask的个人博客系统
- 计算机视觉应用开发流程
- python 调用sftp断点续传文件
- python socket游戏
- 基于Python爬虫爬取天气预报信息
- python函数编程和讲解
- Python开发的个人博客
- 基于python的三层神经网络模型搭建
- python实现自动操作windows应用
- python人脸识别(opencv)
- python 绘图(方形、线条、圆形)
- python疫情卡UN管控
- python 连连看小游戏源码
评论
共有 条评论