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配置文件解析模块ConfigParser使用实例
Apr 13 Python
python中利用h5py模块读取h5文件中的主键方法
Jun 05 Python
Python爬虫常用小技巧之设置代理IP
Sep 13 Python
pycharm中使用anaconda部署python环境的方法步骤
Dec 19 Python
python代码打印100-999之间的回文数示例
Nov 24 Python
python中with语句结合上下文管理器操作详解
Dec 19 Python
tensorflow2.0与tensorflow1.0的性能区别介绍
Feb 07 Python
Pandas时间序列:时期(period)及其算术运算详解
Feb 25 Python
解决python Jupyter不能导入外部包问题
Apr 15 Python
基于python连接oracle导并出数据文件
Apr 28 Python
如何基于Django实现上下文章跳转
Sep 16 Python
如何用python实现一个HTTP连接池
Jan 14 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
PHP 和 MySQL 开发的 8 个技巧
2006/10/09 PHP
PHP 异步执行方法,模拟多线程的应用分析
2013/06/03 PHP
关于php循环跳出的问题
2013/07/01 PHP
php中单个数据库字段多列显示(单字段分页、横向输出)
2014/07/28 PHP
PHP嵌套输出缓冲代码实例
2015/05/12 PHP
PHP定时任务获取微信access_token的方法
2016/10/10 PHP
php实现通过stomp协议连接ActiveMQ操作示例
2020/02/23 PHP
Ext.MessageBox工具类简介
2009/12/10 Javascript
JavaScript高级程序设计 学习笔记 js高级技巧
2011/09/20 Javascript
node.js中的fs.unlink方法使用说明
2014/12/15 Javascript
jQuery鼠标经过方形图片切换成圆边效果代码分享
2015/08/20 Javascript
浅谈JavaScript中的apply/call/bind和this的使用
2017/02/26 Javascript
利用angularjs1.4制作的简易滑动门效果
2017/02/28 Javascript
Vue监听数组变化源码解析
2017/03/09 Javascript
JS使用new操作符创建对象的方法分析
2019/05/30 Javascript
vue-cli3.X快速创建项目的方法步骤
2019/11/14 Javascript
JS实现图片切换特效
2019/12/23 Javascript
js实现登录拖拽窗口
2020/02/10 Javascript
解析Python中while true的使用
2015/10/13 Python
python排序函数sort()与sorted()的区别
2018/09/18 Python
简单了解python单例模式的几种写法
2019/07/01 Python
Python (Win)readline和tab补全的安装方法
2019/08/27 Python
Django Admin设置应用程序及模型顺序方法详解
2020/04/01 Python
canvas像素画板的实现代码
2018/11/21 HTML / CSS
html5中监听canvas内部元素点击事件的三种方法
2019/04/28 HTML / CSS
中专生自荐信
2013/10/12 职场文书
护理专业学生的求职信范文
2013/12/11 职场文书
小学语文课后反思精选
2014/04/25 职场文书
领导干部保密承诺书
2014/08/30 职场文书
乡镇挂职心得体会
2014/09/04 职场文书
党员评议思想汇报
2014/10/08 职场文书
商家认证委托书格式
2014/10/16 职场文书
2014教师年度思想工作总结
2014/11/10 职场文书
供应商食品安全承诺书
2015/04/29 职场文书
单位工资证明范本
2015/06/12 职场文书
奠基仪式致辞
2015/07/30 职场文书