首页 > 编程语言 > 详细

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

时间:2015-08-13 23:42:54      阅读:389      评论:0      收藏:0      [点我收藏+]

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统计文本里单词出现次数

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

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!