题目

你有一个目录,放了你一个月的日记,都是 txt,为了避免分词的问题,假设内容都是英文,请统计出你认为每篇日记最重要的词。


很难客观的说每篇日记中最重要的词是什么,所以在这里就仅仅是将每篇日记中出现频数最高的词作为最重要的词。同时过滤掉一些词诸如【I,is,are,has,and,or】等等

代码

# -*- coding: utf-8 -*-
from collections import Counter
import re
import string
import os def get_word(filepath):
#过滤词汇
filter_word=['she','i','the','is','are','you','we','and','to','or','that','what','has',
'have','been','do','did','a','an',"'",'he','of','was','had','they','his','in','on',
'She','were','it','Mrs','The'] fp=open(filepath,'r')
content=fp.read() rule='[a-zA-Z0-9\']+'
words=re.findall(rule,content)
wordlist = Counter(words)
for i in filter_word:
wordlist[i]=0
fp.close() # most_common 按出现数次从高到底排序
return wordlist.most_common()[0] def get_file(path):
for textname in os.listdir(path):
textfile=os.path.join(path,textname)
most_important=get_word(textfile)
print("文章 ---{} ----统计".format(textname))
print("最重要的词为:{}".format(most_important[0]))
print("出现次数为:{}\n".format(repr(most_important[1]))) if __name__ == '__main__':
get_file('Text')

PS:代码很多场景无法适应,比如出现中文字符,可以更好的完善做一个格式化的字数统计工具

Python每日一题 007的更多相关文章

  1. Python:每日一题007

    题目: 输出 9*9 乘法口诀表. 程序分析: 分行与列考虑,共9行9列,i控制行,j控制列. 个人思路及代码: 第一版: for i in range(1,10): for j in range(1 ...

  2. Python每日一题 004

    将 0001 题生成的 200 个激活码(或者优惠券)保存到 Redis 非关系型数据库中. 代码 import redis import uuid # 创建实例 r=redis.Redis(&quo ...

  3. Python每日一题 003

    将 002 题生成的 200 个激活码(或者优惠券)保存到 MySQL 关系型数据库中. 代码 import pymysql import uuid def get_id(): for i in ra ...

  4. Python每日一题 002

    做为 Apple Store App 独立开发者,你要搞限时促销,为你的应用生成激活码(或者优惠券),使用 Python 如何生成 200 个激活码(或者优惠券)? 在此生成由数字,字母组成的20位字 ...

  5. Python每日一题 009

    题目 有个目录,里面是你自己写过的程序,统计一下你写过多少行代码.包括空行和注释,但是要分别列出来. 代码 参照网络上代码 # coding: utf-8 import os import re # ...

  6. Python每日一题 008

    题目 基于多线程的网络爬虫项目,爬取该站点http://www.tvtv.hk 的电视剧收视率排行榜 分析 robots.txt User-agent: Yisouspider Disallow: / ...

  7. Python每日一题 006

    题目 你有一个目录,装了很多照片,把它们的尺寸变成都不大于 iPhone5 分辨率的大小. 如果只是单纯的通过将图片缩放到iPhone5分辨率大小,显然最后呈现出来的效果会很糟糕.所以等比例缩放到长( ...

  8. Python每日一题 005

    任一个英文的纯文本文件,统计其中的单词出现的个数. 代码 # coding:utf-8 import re def get_word(filename): fp=open(filename," ...

  9. Python每日一题 001

    Github地址:https://github.com/Yixiaohan/show-me-the-code Talk is Cheap, show me the code. --Linus Torv ...

随机推荐

  1. HDU 6073 Matching In Multiplication —— 2017 Multi-University Training 4

    Matching In Multiplication Time Limit: 6000/3000 MS (Java/Others)    Memory Limit: 524288/524288 K ( ...

  2. Apache搭建http网站服务器入门教程

    Apache搭建http网站服务器入门教程 准备工具 一台带有Linux系统的主机,这里使用CentOS 7.1 64位系统 一个备案过的域名,这里使用www.hellopage.cn 一台可以访问网 ...

  3. 1-什么是 Prometheus

    什么是 Prometheus Prometheus 是由 SoundCloud 开源监控告警解决方案,从 2012 年开始编写代码,再到 2015 年 github 上开源以来,已经吸引了 9k+ 关 ...

  4. 实现自己的shell--MIT xv6 shell

    参考代码: #include <stdlib.h> #include <unistd.h> #include <stdio.h> #include <fcnt ...

  5. (65)C# 任务

    1.启动任务 //Framework4.5新增的Task.Run开启一个任务,Run方法中传入一个Action委托 Task.Run(()=> { Thread.Sleep(); Console ...

  6. datastudion 资源导入python包,编写模块

    学习文档,不懂再问. https://help.aliyun.com/document_detail/74423.html?spm=a2c4g.11186623.6.688.72635debHqgkV ...

  7. Mr. Panda and Crystal(最短路+完全背包)

    http://codeforces.com/gym/101206/attachments 题意: T组输入,每组给出m,n,k,m为能量总数,n为水晶种类数,k为合成方案数.有的水晶可以用能量制造,有 ...

  8. python 装饰器 第二步:扩展函数的功能(不修改原函数)

    # 第二步:扩展函数的功能(不能修改原函数) # 用于扩展基本函数的函数 # 把一个函数(eat函数)作为一个整体传给另外一个函数(kuozhan函数) # 这个函数(kuozhan函数)用形参fun ...

  9. 原生js事件绑定

    一.JS事件 (一)JS事件分类 1.鼠标事件: click/dbclick/mouseover/mouseout 2.HTML事件: onload/onunload/onsubmit/onresiz ...

  10. 多线程--ThreadLocal类

    一.ThreadLocal类简介--此类是在整个开发过程中至关重要的类,他主要是在开发过程中解决了核心资源和多线程并发访问的处理情况--在真正去了解ThreadLocal类作用的时候,我们可以先编写一 ...