题目来自:Python 练习册。今天做第四题:任一英文的纯文本文件,统计其中的单词出现个数。

铺垫工作

这一期的铺垫工作比较多,所以单独写了一篇文章,详见 Python正则表达式

正文部分

题目内容

任一个英文的纯文本文件,统计其中的单词出现的个数。

参考英文:If you are looking for someone you can pour out your love to, let me suggest the empowered woman. The empowered woman knows what she wants, knows how to get it, knows how to live fully, and she knows how to love you back without needing anyone’s approval or recognition. An empowered woman is unarguably one of the most magnificent beings you will ever come in contact with. Read on and find 10 reason why you should absolutely love and embrace the empowered women in your life! .

1. She knows how to love you in returnIt is difficult to give what you don’t have. It is impossible to love someone and feel fulfilled when they can’t love you in return because they don’t love themselves. This will never happen to you when you love an empowered woman. She loves herself (not in a narcissistic manner). In turn, she appreciates who you are and loves you in return. She will love you just like you deserve to be loved.

2. She will inspire youWhen life puts you down and you are at the end of your rope, the empowered woman will be there to see you through. Her drive, enthusiasm and (at times) hopeless optimism will inspire you to carry on despite the obstacles you face.

3. She is not afraid of failureWhile many out there are thoroughly terrified of failure, the empowered woman understands that failures are simply stepping stones in life. How can you not love someone that is thoroughly unafraid to try, fail, and give it a shot all over again?!

4. She is all about the legacyWhile most people are focused on the car, the house, the job, and corner office; the empowered woman is focused on leaving a legacy that will inspire others and change the world. The empowered woman is focused on empowering others to maximize their potential and fulfill their purpose. She is all about inspiring others to look beyond themselves and live a life of service to others.

5. She can laugh at her mistakes……and learn from them as well! She understands mistakes are part of the journey. The empowered woman can laugh and learn from her mistakes to ensure they never happen again.

6. She can be vulnerableThe empowered woman understands there is no debt in relationships without vulnerability. Although she is emotionally strong, she is willing to laugh and cry with you because all of these emotions are an essential part of life.

7. She can speak her mindWhile everyone else is too concerned with what others may think or say, the empowered woman is not afraid to speak her mind. She understands that her value comes from within, not from what others say or think about her.

8. She knows when to remain quietShe lives by Abe Lincoln’s words, “Better to remain silent and be thought a fool, than to speak out and remove all doubt.”

9. She knows how to have funWhether it is at the symphony or at a ball game, the empowered woman understands life is made up of experiences with people – not the places you go. She is able to live in the moment and enjoy it fully without being concerned for the future. After all, who’s got a guaranteed future?

10. She is not afraid of changeWhile most people rather continue on living unfulfilled lives as long as their comfort zone remains intact, the empowered woman is all about embracing change. She understands growth cannot happen without change. She understands that change is the gift life offers you to choose your destiny. Therefore, she is not afraid of change because it is her stepping stone towards success.

下载链接

将文件下载到python的工作路径里去,如果不知道哪里是工作路径,输入import os

#获取当前工作目录

os.getcwd()

#更改当前工作目录

os.chdir('d:\')

os.getcwd()

参考代码

每一步我都尽量附带上了解释# coding=utf-8

from collections import defaultdict

import re

# 替换除了n't这类连字符外的所有非单词字符和数字字符

def replace(s):

if s.group(1) == 'n\'t':

return s.group(1)

return ' '

def cal(filename='203305485.txt'):

# 使用lambda来定义简单的函数

dic = defaultdict(lambda: 0)#dic = defaultdict(int)也可以

with open(filename, 'r') as f:

data = f.read()

# 全部变为小写字母

data = data.lower()

# 替换除了n't这类连字符外的所有非单词字符和数字字符

data = re.sub(r'(n[\']t)|([\W\d])', replace, data)

datalist = re.split(r'[\s\n]+', data)

for item in datalist:

dic[item] += 1

del dic['']

return dic

if __name__ == '__main__':

dic = cal()

for key, val in dic.items():

print('%15s ----> %3s' % (key,val))

运行结果如下:

增加排序函数

代码有参考 《利用python进行数据分析》def top_counts(dic, n=10):

value_key_pairs = [(count, tz) for tz, count in dic.items()]

value_key_pairs.sort()

return value_key_pairs[-n:]

top_counts(dic)

运行结果如下:

可以看出,人们最喜欢用的词是定冠词the,下来是介词to.......

补充

最近发现collections模块的Counter类 ,

导入语句是:from collections import Counter,作用是:定义一个list数组,求数组中每个元素出现的次数

修改之后代码量要少很多,而且可以直接排列好顺序~# coding=utf-8

import re

from collections import Counter

def cal(filename='203305485.txt'):

with open(filename, 'r') as f:

data = f.read()

data = data.lower()

# 替换除了n't这类连字符外的所有非单词字符和数字字符

datalist = re.split(r'[\s\n]+', data)

return Counter(datalist).most_common()

if __name__ == '__main__':

dic = cal()

for i in range(len(dic)):

print('%15s ----> %3s' % (dic[i][0],dic[i][1]))

代码看起来行云流水,舒服多了。当然结论是一样的,人们还是比较喜欢说 the , you~

以上~

本文由 mmmwhy 创作,最后编辑时间为: May 2, 2019 at 01:53 pm

用python统计单词出现的个数_第1.4题:统计文件中单词出现个数相关推荐

  1. python统计有几个单词_统计文件中单词的个数---Shell及python版

    最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash if [ $# -ne ];then echo "Usage:basename $0 fil ...

  2. python文件中单词的删除_使用python删除文件中的多余单词

    嗨,我正在学习Python,出于好奇,我编写了一个程序来删除文件中多余的单词. 我正在比较文件text1.txt中的测试.和'text2.txt',基于text1中的测试,我删除了test2中多余的单 ...

  3. 统计文件字符个数 java_如何统计个文件中的字符个数

    一个朋友问到了统计文件字符数的问题.就找了两个. import java.io.*; public class Execute_char { final int MAX = 9999; int len ...

  4. linux统计某个字符个数,Linux统计一个文件中特定字符个数的方法

    统计一个文件中某个字符串的个数,其实就是在在一块沙地里面找石头,有的人看到石头以后,在上面做个标记(grep),然后记住自己做了多少个标记:有的人看到石头以后,把它挖了(tr),最后统计自己挖了多少石 ...

  5. 【数据结构笔记42】哈希表应用:文件中单词词频统计

    本次笔记内容: 11.5 文件中单词词频统计 文章目录 题目 分析 程序框架 题目 如上图,对单词词频进行统计. 分析 如上图,涉及到对已有单词进行查找,因此要进行单词的管理,使用散列表. 程序框架 ...

  6. 简单的MapReduce项目,计算文件中单词出现的次数

    简单的MapReduce项目,计算文件中单词出现的次数 计算文件中单词出现的次数,试题如下图 1.创建读取单词的文件tast,内容如下: hadoop core map reduce hiv hba ...

  7. Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中

    Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中 目录 Python使用matplotlib可视化绘制并导出可视化结果图表到PDF文件中

  8. C语言文件操作(二)对指定txt文件中的N个数排序

    #include<stdio.h> #include<stdlib.h> #include<time.h> #include<conio.h> #def ...

  9. Python练手小程序—统计英文文件中单词出现的的个数

    在GitHub上发现一些很有意思的项目,由于本人作为Python的初学者,编程代码能力相对薄弱,为了加强Python的学习,特此利用前辈们的学习知识成果,自己去亲自实现. 一周没有更新了,主要还是自己 ...

  10. python做线性回归统计推断提取参数_概率分析方法与推断统计(来自我写的python书)...

    在数据分析统计的场景里,常用的方法除了描述性统计方法外,还有推断统计方法,如果再从工作性质上来划分,推断统计包含了参数估计和假设验证这两方面的内容.而推断统计用到了很多概率统计方法,所以本小节在介绍推 ...

最新文章

  1. Windows实现appium+iOS自动化测试
  2. python软件下载路径问题-mac上Python安装和修改Python默认路径遇到的问题
  3. oracle构造过程实例
  4. 成功解决UnicodeDecodeError: ‘utf-8‘ codec can‘t decode byte 0xd3 in position 238: invalid continuation b
  5. 为工厂分配用于公司间开票的销售范围
  6. Qt Creator设置Qbs
  7. Ubuntu 键盘错位解决 更改键盘布局
  8. 安卓手机能用吗_手机才用两年卡的不行,是手机问题吗,想问手机最长能用几年?...
  9. 京东面试官:SQL 语句中 left join 后用 on 还是 where,区别大了!
  10. sai笔记1-sai安装
  11. Openrefine mysql_openrefine 2.5稳定版-OpenRefine下载(数据清洗工具) 2.5 官方稳定版 - 河东下载站...
  12. 过采样方法、欠采样介绍
  13. ARM和Linux下 nanomsg 编译与使用
  14. java前台显示后台数据_Thymeleaf在后台获取数据在前台显示
  15. Map集合通过value获取key的几种方式
  16. glm-0.9.9.7 + visual studio 2019 + window10下载安装
  17. 获取百度首页的源代码
  18. 现代的linux和windows7,Windows 7 Vs. Linux谁更强
  19. 哔哩哔哩第三方神器软件,早该用上了
  20. emoji昵称php,php过滤微信用户昵称emoji表情方法汇总 - 旗云号

热门文章

  1. 龙叔学ES:elasticsearch扫盲篇
  2. 再次解决,android 2.3运行凯立德问题
  3. java实现操作系统时间片轮转进程调度算法(RR算法)
  4. 写一个MySql存储过程实现房贷等额本息还款计算(另外附javascript代码)
  5. 大同煤矿生产用水无线监测方案
  6. 求解直线与平面的交点
  7. 生动有趣的“用户体验与产品设计”公开课!
  8. codevs1384黑色星期五【USACO】
  9. 公众号 H5页面分享链接携带图片与标题(自定义卡片式分享链接)
  10. 编译原理MOOC笔记