亚洲最大看欧美片,亚洲图揄拍自拍另类图片,欧美精品v国产精品v呦,日本在线精品视频免费

  • 站長(zhǎng)資訊網(wǎng)
    最全最豐富的資訊網(wǎng)站

    python統(tǒng)計(jì)單詞出現(xiàn)次數(shù)

    python統(tǒng)計(jì)單詞出現(xiàn)次數(shù)

    python統(tǒng)計(jì)單詞出現(xiàn)次數(shù)

    做單詞詞頻統(tǒng)計(jì),用字典無(wú)疑是最合適的數(shù)據(jù)類(lèi)型,單詞作為字典的key, 單詞出現(xiàn)的次數(shù)作為字典的 value,很方便地就記錄好了每個(gè)單詞的頻率,字典很像我們的電話(huà)本,每個(gè)名字關(guān)聯(lián)一個(gè)電話(huà)號(hào)碼。

    下面是具體的實(shí)現(xiàn)代碼,實(shí)現(xiàn)了從importthis.txt文件讀取單詞,并統(tǒng)計(jì)出現(xiàn)次數(shù)最多的5個(gè)單詞。

    # -*- coding:utf-8 -*- import io import re  class Counter:     def __init__(self, path):         """         :param path: 文件路徑         """         self.mapping = dict()         with io.open(path, encoding="utf-8") as f:             data = f.read()             words = [s.lower() for s in re.findall("w+", data)]             for word in words:                 self.mapping[word] = self.mapping.get(word, 0) + 1      def most_common(self, n):         assert n > 0, "n should be large than 0"         return sorted(self.mapping.items(), key=lambda item: item[1], reverse=True)[:n]  if __name__ == '__main__':     most_common_5 = Counter("importthis.txt").most_common(5)     for item in most_common_5:         print(item)

    執(zhí)行效果:

    ('is', 10) ('better', 8) ('than', 8) ('the', 6) ('to', 5)

    贊(0)
    分享到: 更多 (0)
    網(wǎng)站地圖   滬ICP備18035694號(hào)-2    滬公網(wǎng)安備31011702889846號(hào)