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抓取百度查询结果的方法
Jul 08 Python
python比较两个列表大小的方法
Jul 11 Python
对python中raw_input()和input()的用法详解
Apr 22 Python
python利用Tesseract识别验证码的方法示例
Jan 21 Python
Laravel+Dingo/Api 自定义响应的实现
Feb 17 Python
python 定时器,实现每天凌晨3点执行的方法
Feb 20 Python
python里运用私有属性和方法总结
Jul 08 Python
Python中zip()函数的简单用法举例
Sep 02 Python
Keras: model实现固定部分layer,训练部分layer操作
Jun 28 Python
golang/python实现归并排序实例代码
Aug 30 Python
python中复数的共轭复数知识点总结
Dec 06 Python
利用Python实现自动扫雷小脚本
Dec 17 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 增加了对 .ZIP 文件的读取功能
2006/10/09 PHP
PHP新手上路(八)
2006/10/09 PHP
PHP动态变静态原理
2006/11/25 PHP
Yii框架form表单用法实例
2014/12/04 PHP
thinkphp在php7环境下提示Cannot use ‘String’ as class name as it is reserved的解决方法
2016/09/30 PHP
JavaScript 创建对象
2009/07/17 Javascript
Js 随机数产生6位数字
2010/05/13 Javascript
javascript操作table(insertRow,deleteRow,insertCell,deleteCell方法详解)
2013/12/16 Javascript
影响jQuery使用的14个方面
2014/09/01 Javascript
jQuery日程管理插件fullcalendar使用详解
2017/01/07 Javascript
vue.js实现条件渲染的实例代码
2017/06/22 Javascript
AngularJs点击状态值改变背景色的实例
2017/12/18 Javascript
Vue2.0 实现移动端图片上传功能
2018/05/30 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
详解微信小程序框架wepy踩坑记录(与vue对比)
2019/03/12 Javascript
webpack4实现不同的导出类型
2019/04/09 Javascript
vue 实现通过vuex 存储值 在不同界面使用
2019/11/11 Javascript
js获取图片的base64编码并压缩
2020/12/05 Javascript
python3.X 抓取火车票信息【修正版】
2018/06/19 Python
python实现图片识别汽车功能
2018/11/30 Python
使用Python快速制作可视化报表的方法
2019/02/03 Python
Python写一个基于MD5的文件监听程序
2019/03/11 Python
Django中使用极验Geetest滑动验证码过程解析
2019/07/31 Python
keras slice layer 层实现方式
2020/06/11 Python
如何清空python的变量
2020/07/05 Python
HTML5中的Scoped属性使用实例
2014/04/23 HTML / CSS
详解HTML5 录音的踩坑之旅
2017/12/26 HTML / CSS
Vuori官网:运动服装的终级表现
2021/01/27 全球购物
面试求职的个人自我评价
2013/11/16 职场文书
服务宗旨标语
2014/07/01 职场文书
红领巾广播站广播稿(3篇)
2014/09/20 职场文书
教师学习中国梦心得体会
2016/01/05 职场文书
Python控制台输出俄罗斯方块移动和旋转功能
2021/04/18 Python
用Python爬取某乎手机APP数据
2021/06/15 Python
总结Java对象被序列化的两种方法
2021/06/30 Java/Android
Win10鼠标轨迹怎么开 Win10显示鼠标轨迹方法
2022/04/06 数码科技