5条回答 默认 最新
CSDN专家-HGJ 2021-05-10 21:17关注用jieba库进行分词操作即可。
import jieba # 调用jieba库 txt = open(r"sanguo.txt","r", encoding="gbk").read() # 打开txt文件阅读 words = jieba.cut(txt) # 分词 counts = {} # 建立一个空字典 for word in words: if len(word) == 1: continue else: counts[word] = counts.get(word, 0) + 1 # 遍历每个中文单词,通过字典来计数 #items = list(counts.items()) # 将字典counts转换为列表类型 #items.sort(key=lambda x: x[1], reverse=True) # 对列表进行排序 inp=input('请输入姓名:') if inp in counts: print(inp,counts[inp])#输出找到的人名及对应次数 else: print('书中没有找到。')本回答被题主选为最佳回答 , 对您是否有帮助呢?解决 无用评论 打赏 举报