第0004道練習題_Python統計文本里單詞出現次數

Python練習題第 0004 題

https://github.com/Show-Me-the-Code/show-me-the-code
第 0004 題:任一個英文的純文本文件,統計其中的單詞出現次數。

Talk is cheap, show you my code.

#! /usr/bin/env python
#! -*- coding: utf-8 -*-

from collections import OrderedDict
__author__ = 'Sophie'

class AppearanceCounter(object):
    def __init__(self):
        self.dict = {}

    def add_count(self,item):
        count = self.dict.setdefault(item, 0)
        self.dict[item] = count + 1

    def sort(self, desc = None):

        """~~~~~~Method 1~~~~~~~~~"""
        #result = sorted([(v,k) for (k,v) in self.dict.items()], reverse = desc)

        """~~~~~~Method 2~~~~~~~~~"""
        result = OrderedDict(sorted(self.dict.items(), key = lambda x: x[1], reverse = desc))

        return result

if __name__ == '__main__':
    ac = AppearanceCounter()
    file = open('/Users/Sophie/PycharmProjects/Practice_0004/CNN_News.txt','r')
    try:
        list_of_all_lines = file.readlines()
    finally:
        file.close()

    list_of_all_words = []
    temp = []

    for x in list_of_all_lines:
        temp = [t.strip(".?\"!,()'") for t in x.lower().split()]
        list_of_all_words.extend(temp)

    for x in list_of_all_words:
        ac.add_count(x)

    r = ac.sort(True)
    print r

小知識點Get

1、setdefault(key[, default])
If key is in the dictionary, return its value. If not, insert key with a value of default and return default. default defaults to None.
這個很好用,如果key已經存在於字典中,返回它對應的value,如果key不存在,則插入key和default value

2、我的字典裏面有很多key-value對,如何排序?
http://stackoverflow.com/questions/613183/sort-a-python-dictionary-by-value

發佈了66 篇原創文章 · 獲贊 14 · 訪問量 9萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章