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使用多线程不断刷新网页的方法
Mar 31 Python
在Python中使用dict和set方法的教程
Apr 27 Python
对比Python中__getattr__和 __getattribute__获取属性的用法
Jun 21 Python
python中requests小技巧
May 10 Python
python中(str,list,tuple)基础知识汇总
Feb 20 Python
Python中return self的用法详解
Jul 27 Python
python DataFrame 取差集实例
Jan 30 Python
详解python列表(list)的使用技巧及高级操作
Aug 15 Python
django框架创建应用操作示例
Sep 26 Python
python实现学生管理系统开发
Jul 24 Python
Django路由层URLconf作用及原理解析
Sep 24 Python
python中random模块详解
Mar 01 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访问数据库集群的方法小结
2016/03/14 PHP
Laravel timestamps 设置为unix时间戳的方法
2019/10/11 PHP
javascript 类定义的4种方法
2009/09/12 Javascript
Javascript 静态页面实现随机显示广告的办法
2010/11/17 Javascript
jquery显示和隐藏div特效实例
2013/02/27 Javascript
js使用for循环与innerHTML获取选中tr下td值
2014/09/26 Javascript
jQuery ajax serialize() 方法使用示例
2014/11/02 Javascript
分享20个提升网站界面体验的jQuery插件
2014/12/15 Javascript
基于JavaScript实现的折半查找算法示例
2017/04/14 Javascript
微信小程序-滚动消息通知的实例代码
2017/08/03 Javascript
使用 Javascript 实现浏览器推送提醒功能的示例
2017/11/03 Javascript
解决jQuery使用append添加的元素事件无效的问题
2018/08/30 jQuery
JavaScript实现简单的隐藏式侧边栏功能示例
2018/08/31 Javascript
Vue.js 十五分钟入门图文教程
2018/09/12 Javascript
qrcode生成二维码微信长按无法识别问题的解决
2019/04/04 Javascript
原生javascript单例模式的应用实例分析
2020/02/23 Javascript
vue+element-ui JYAdmin后台管理系统模板解析
2020/07/28 Javascript
[58:42]DOTA2上海特级锦标赛C组败者赛 Newbee VS Archon第一局
2016/02/27 DOTA
python自定义解析简单xml格式文件的方法
2015/05/11 Python
python实现自动重启本程序的方法
2015/07/09 Python
Python和Perl绘制中国北京跑步地图的方法
2016/03/03 Python
Python对列表去重的多种方法(四种方法)
2017/12/05 Python
简单谈谈python中的lambda表达式
2018/01/19 Python
用Python写脚本,实现完全备份和增量备份的示例
2018/04/29 Python
关于Pycharm无法debug问题的总结
2019/01/19 Python
对Python3 pyc 文件的使用详解
2019/02/16 Python
如何解决cmd运行python提示不是内部命令
2020/07/01 Python
利用CSS3的线性渐变linear-gradient制作边框的示例
2016/06/02 HTML / CSS
马来西亚与新加坡长途巴士售票网站:BusOnlineTicket.com
2018/11/05 全球购物
护士自我鉴定范文
2013/10/06 职场文书
招标保密承诺书
2015/01/20 职场文书
党员公开承诺书2015
2015/01/21 职场文书
社区活动总结范文
2015/05/07 职场文书
《比的意义》教学反思
2016/02/18 职场文书
html中相对位置与绝对位置的具体使用
2022/05/15 HTML / CSS
SQL解决未能删除约束问题drop constraint
2022/05/30 SQL Server