Python3实现统计单词表中每个字母出现频率的方法示例


Posted in Python onJanuary 28, 2019

本文实例讲述了Python3实现统计单词表中每个字母出现频率的方法。分享给大家供大家参考,具体如下:

作为python字典与数组概念的运用,统计字母表中每个字母出现的频率,作为练习再合适不过。

解决问题过程中需要用到的知识点包括:字典的创建、增添元素,数组的创建、增添元素,数组的遍历等

这个问题解决的思路为:首先从文件中按行依次读入单词,去除换行符后添加到数组 new_list 中。依次遍历数组 new_list 的每一个字符串,将每个字符串连同上一次循环中的频率统计结果 old_d (old_d在遍历new_list之前进行初始化)一起作为实参传递给频率统计函数 histogram()。histogram()函数在上一轮频率统计基础上得出本轮频率统计结果,结果通过字典 d 传回,将值赋给 old_d 。直到遍历完new_list,再将 old_d 统计结果打印。

'''transform string into dictionary
s is input string
d is dictionary to restore every bit in string
'''
def histogram(s, old_d):
  d = old_d
  for c in s:
    d[c] = d.get(c, 0) + 1
  return d
'''This function can calculate the frequency of every letter in alphabet
'''
fin = open("words.txt")
new_list = []
for line in fin:
  rs = line.rstrip('\n') #delete the '\n' after every letter
  new_list.append(rs) # new_list is used to restore letters
old_d = dict() # initialize the dictionary
for i in range(len(new_list)): #calculate the letter
#frequency of every word
  old_d = histogram(new_list[i], old_d) #old_d is used to
  #restore letter frequency before new_list[i]
print(old_d)

这里words.txt文档内容如下:

But soft what light through yonder window breaks
It is the east and Juliet is the sun
Arise fair sun and kill the envious moon
Who is already sick and pale with grief

代码运行结果:

{'B': 1, 'u': 6, 't': 12, ' ': 29, 's': 11, 'o': 8, 'f': 3, 'w': 4, 'h': 9, 'a': 10, 'l': 6, 'i': 13, 'g': 3, 'r': 7, 'y': 2, 'n': 9, 'd': 6, 'e': 12, 'b': 1, 'k': 3, 'I': 1, 'J': 1, 'A': 1, 'v': 1, 'm': 1, 'W': 1, 'c': 1, 'p': 1}

Python 相关文章推荐
详解Python中DOM方法的动态性
Apr 11 Python
Python的函数的一些高阶特性
Apr 27 Python
Python两个内置函数 locals 和globals(学习笔记)
Aug 28 Python
python 生成器协程运算实例
Sep 04 Python
python不换行之end=与逗号的意思及用途
Nov 21 Python
python pandas 对时间序列文件处理的实例
Jun 22 Python
Django model序列化为json的方法示例
Oct 16 Python
python 列表转为字典的两个小方法(小结)
Jun 28 Python
python批量处理txt文件的实例代码
Jan 13 Python
python使用python-pptx删除ppt某页实例
Feb 14 Python
python str字符串转uuid实例
Mar 03 Python
Python中json.dumps()函数的使用解析
May 17 Python
Python判断变量名是否合法的方法示例
Jan 28 #Python
Python使用while循环花式打印乘法表
Jan 28 #Python
Python实现程序判断季节的代码示例
Jan 28 #Python
Python后台管理员管理前台会员信息的讲解
Jan 28 #Python
Python之列表实现栈的工作功能
Jan 28 #Python
Python中常用的内置方法
Jan 28 #Python
Python中的元组介绍
Jan 28 #Python
You might like
PHP curl 并发最佳实践代码分享
2012/09/05 PHP
Thinkphp中的curd应用实用要点
2015/01/04 PHP
PHP递归遍历指定目录的文件并统计文件数量的方法
2015/03/24 PHP
Smarty环境配置与使用入门教程
2016/05/11 PHP
PHP房贷计算器实例代码,等额本息,等额本金
2017/04/01 PHP
图像替换新技术 状态域方法
2010/01/28 Javascript
JSON格式化输出
2014/11/10 Javascript
jQuery选择器用法实例详解
2015/12/17 Javascript
深入理解React中es6创建组件this的方法
2016/08/29 Javascript
Vue声明式渲染详解
2017/05/17 Javascript
微信小程序 跳转传递数据的实例
2017/07/06 Javascript
JS实现浏览上传文件的代码
2017/08/23 Javascript
prototype.js简单实现ajax功能示例
2017/10/18 Javascript
jQuery实现ajax回调函数带入参数的方法示例
2018/06/26 jQuery
NProgress显示顶部进度条效果及使用详解
2019/09/21 Javascript
Python的Flask框架及Nginx实现静态文件访问限制功能
2016/06/27 Python
Python判断两个对象相等的原理
2017/12/12 Python
Python针对给定列表中元素进行翻转操作的方法分析
2018/04/27 Python
python去掉 unicode 字符串前面的u方法
2018/10/21 Python
python requests.post带head和body的实例
2019/01/02 Python
python的依赖管理的实现
2019/05/14 Python
python Django中models进行模糊查询的示例
2019/07/18 Python
基于django ManyToMany 使用的注意事项详解
2019/08/09 Python
Python实现word2Vec model过程解析
2019/12/16 Python
如何在django中实现分页功能
2020/04/22 Python
Python datetime模块使用方法小结
2020/06/18 Python
大三学生做职业规划:给未来找个方向
2014/02/24 职场文书
《登鹳雀楼》教学反思
2014/04/09 职场文书
小学生环保演讲稿
2014/04/25 职场文书
材料物理专业求职信
2014/09/01 职场文书
营销经理工作检讨书
2014/11/03 职场文书
防卫过当辩护词
2015/05/21 职场文书
彻底理解golang中什么是nil
2021/04/29 Golang
如何利用Python实现一个论文降重工具
2021/07/09 Python
ORM模型框架操作mysql数据库的方法
2021/07/25 MySQL
Pygame Rect区域位置的使用(图文)
2021/11/17 Python