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
版权声明:本文为博主原创文章,未经博主允许不得转载。
时间: 2024-12-21 18:23:33