码迷,mamicode.com
首页 > 编程语言 > 详细

第0004道练习题_Python统计文本里单词出现次数

时间:2015-08-13 23:44:36      阅读:244      评论:0      收藏:0      [点我收藏+]

标签: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

版权声明:本文为博主原创文章,未经博主允许不得转载。

第0004道练习题_Python统计文本里单词出现次数

标签:python

原文地址:http://blog.csdn.net/sophie2805/article/details/47623315

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!