)
运行代码import jiebatxt open(C:/Users/3574045633/Desktop/红楼梦.txt, r, encodinggb18030).read()words jieba.lcut(txt)counts {}alias_map {宝玉: 贾宝玉, 宝二爷: 贾宝玉, 怡红公子: 贾宝玉,黛玉: 林黛玉, 颦儿: 林黛玉, 林妹妹: 林黛玉,宝钗: 薛宝钗, 宝姑娘: 薛宝钗, 宝姐姐: 薛宝钗,凤姐: 王熙凤, 凤辣子: 王熙凤, 琏二奶奶: 王熙凤,老太太: 贾母, 史太君: 贾母, 老祖宗: 贾母,太太: 王夫人,探春: 贾探春, 三姑娘: 贾探春,湘云: 史湘云,袭人: 袭人, 花袭人: 袭人,姥姥: 刘姥姥,}for word in words:if len(word) 1:continueif word in alias_map:word alias_map[word]counts[word] counts.get(word, 0) 1items list(counts.items())items.sort(keylambda x:x[1], reverseTrue)for i in range(20):word, count items[i]print({0:10}{1:5}.format(word, count))输出示例