python与统计(龙族版)

因为我很喜欢龙族,额.......。我也很喜欢python这门语言。然后就结合了一下,用python了解了一下龙族四本书的人物出场次数及排名。

《龙族1火之晨曦》

路明非 1877

诺诺 659

芬格尔 321

恺撒 282

古德里安 230

《龙族2悼亡者之瞳》

路明非 1370

楚子航 1264

昂热 439

恺撒 382

夏弥 229

《龙族3黑月之潮》

路明非 2400

恺撒 2141

源稚生 1545

楚子航 1311

昂热 924

绘梨衣 705

《龙族4奥丁之渊》

路明非 1519

诺诺 994

楚子航 513

芬格尔 300

顺便插个代码

import jieba
txt = open("dragon4.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 == "楚子":
                rword = "楚子航"
        else:
                rword = word
        counts[rword] = counts.get(rword,0) + 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(4):
        word,count = items[i]
        print("{0:<10}{1:>5}".format(word,count))

吐槽一下

有生之年能看龙族5真是人生一大乐事!

原文地址:https://www.cnblogs.com/Martrix-revolution/p/8879159.html