python统计文本字符串里单词出现频率的方法


Posted in Python onMay 26, 2015

本文实例讲述了python统计文本字符串里单词出现频率的方法。分享给大家供大家参考。具体实现方法如下:

# word frequency in a text
# tested with Python24  vegaseat  25aug2005
# Chinese wisdom ...
str1 = """Man who run in front of car, get tired.
Man who run behind car, get exhausted."""
print "Original string:"
print str1
print
# create a list of words separated at whitespaces
wordList1 = str1.split(None)
# strip any punctuation marks and build modified word list
# start with an empty list
wordList2 = []
for word1 in wordList1:
  # last character of each word
  lastchar = word1[-1:]
  # use a list of punctuation marks
  if lastchar in [",", ".", "!", "?", ";"]:
    word2 = word1.rstrip(lastchar)
  else:
    word2 = word1
  # build a wordList of lower case modified words
  wordList2.append(word2.lower())
print "Word list created from modified string:"
print wordList2
print
# create a wordfrequency dictionary
# start with an empty dictionary
freqD2 = {}
for word2 in wordList2:
  freqD2[word2] = freqD2.get(word2, 0) + 1
# create a list of keys and sort the list
# all words are lower case already
keyList = freqD2.keys()
keyList.sort()
print "Frequency of each word in the word list (sorted):"
for key2 in keyList:
 print "%-10s %d" % (key2, freqD2[key2])

希望本文所述对大家的Python程序设计有所帮助。

Python 相关文章推荐
Python循环语句中else的用法总结
Sep 11 Python
python爬虫 正则表达式使用技巧及爬取个人博客的实例讲解
Oct 20 Python
在python中使用正则表达式查找可嵌套字符串组
Oct 24 Python
Python实现的圆形绘制(画圆)示例
Jan 31 Python
python数字图像处理之高级形态学处理
Apr 27 Python
Python封装原理与实现方法详解
Aug 28 Python
pycharm运行和调试不显示结果的解决方法
Nov 30 Python
使用python绘制二元函数图像的实例
Feb 12 Python
使用python将多个excel文件合并到同一个文件的方法
Jul 09 Python
python中seaborn包常用图形使用详解
Nov 25 Python
python redis存入字典序列化存储教程
Jul 16 Python
Python+uiautomator2实现自动刷抖音视频功能
Apr 29 Python
python通过get,post方式发送http请求和接收http响应的方法
May 26 #Python
python使用urllib2提交http post请求的方法
May 26 #Python
Python同时向控制台和文件输出日志logging的方法
May 26 #Python
python实现查找excel里某一列重复数据并且剔除后打印的方法
May 26 #Python
python使用正则表达式提取网页URL的方法
May 26 #Python
python获取指定路径下所有指定后缀文件的方法
May 26 #Python
python通过apply使用元祖和列表调用函数实例
May 26 #Python
You might like
用缓存实现静态页面的测试
2006/12/06 PHP
Apache服务器下防止图片盗链的办法
2015/07/06 PHP
利用php生成验证码
2017/02/23 PHP
PHP正则之正向预查与反向预查讲解与实例
2020/04/06 PHP
prototype 1.5 & scriptaculous 1.6.1 学习笔记
2006/09/07 Javascript
javascript中用星号表示预录入内容的实现代码
2011/01/08 Javascript
Lazy Load 延迟加载图片的jQuery插件中文使用文档
2012/10/18 Javascript
你必须知道的Javascript知识点之"字面量和对应类型"说明介绍
2013/04/23 Javascript
JavaScript的Module模式编程深入分析
2013/08/13 Javascript
jQuery Migrate 1.1.0 Released 注意事项
2014/06/14 Javascript
关闭页面时window.location事件未执行的原因分析及解决方案
2014/09/01 Javascript
用C/C++来实现 Node.js 的模块(二)
2014/09/24 Javascript
js鼠标点击图片实现随机变换图片的方法
2015/02/16 Javascript
JavaScript的Polymer框架中dom-repeat与VM的相关操作
2015/07/29 Javascript
基于dropdown.js实现的两款美观大气的二级导航菜单
2015/09/02 Javascript
jQuery实现每隔一段时间自动更换样式的方法分析
2018/05/03 jQuery
原生js实现form表单序列化的方法
2018/08/02 Javascript
Layui给数据表格动态添加一行并跳转到添加行所在页的方法
2018/08/20 Javascript
微信小程序mpvue点击按钮获取button值的方法
2019/05/29 Javascript
Layui 数据表格批量删除和多条件搜索的实例
2019/09/04 Javascript
Python里disconnect UDP套接字的方法
2015/04/23 Python
Python类的用法实例浅析
2015/05/27 Python
python实现中文分词FMM算法实例
2015/07/10 Python
python3利用Dlib19.7实现人脸68个特征点标定
2018/02/26 Python
Python pyinotify日志监控系统处理日志的方法
2018/03/08 Python
python3.4.3下逐行读入txt文本并去重的方法
2018/04/29 Python
matplotlib subplots 设置总图的标题方法
2018/05/25 Python
python 将print输出的内容保存到txt文件中
2018/07/17 Python
python的faker库用法
2019/11/28 Python
tensorflow 实现自定义梯度反向传播代码
2020/02/10 Python
佳能英国官方网站:Canon UK
2017/08/08 全球购物
清洁工岗位职责
2014/01/29 职场文书
学生安全责任书
2014/04/15 职场文书
综治维稳工作承诺书
2014/08/30 职场文书
2014年团工作总结
2014/11/27 职场文书
Mysql实现主从配置和多主多从配置
2021/06/02 MySQL