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实现面向对像的ASP程序实例
Nov 10 Python
Python验证码识别处理实例
Dec 28 Python
python的random模块及加权随机算法的python实现方法
Jan 04 Python
python中使用print输出中文的方法
Jul 16 Python
详解python实现交叉验证法与留出法
Jul 11 Python
django 控制页面跳转的例子
Aug 06 Python
解决Djang2.0.1中的reverse导入失败的问题
Aug 16 Python
对Django的restful用法详解(自带的增删改查)
Aug 28 Python
Python paramiko模块使用解析(实现ssh)
Aug 30 Python
tensorflow tf.train.batch之数据批量读取方式
Jan 20 Python
python生成大写32位uuid代码
Mar 03 Python
python3.6中anaconda安装sklearn踩坑实录
Jul 28 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
《逃离塔科夫》——“萌新劝退,老手自嗨”的硬核FPS游戏
2020/04/03 其他游戏
5种PHP创建数组的实例代码分享
2014/01/17 PHP
thinkphp,onethink和thinkox中验证码不显示的解决方法分析
2016/06/06 PHP
jQuery 使用手册(四)
2009/09/23 Javascript
JavaScript实现穷举排列(permutation)算法谜题解答
2014/12/29 Javascript
利用JQuery实现datatables插件的增加和删除行功能
2017/01/06 Javascript
jQuery.ajax向后台传递数组问题的解决方法
2017/05/12 jQuery
详解vue.js 开发环境搭建最简单攻略
2017/06/12 Javascript
如何快速解决JS或Jquery ajax异步跨域的问题
2018/01/08 jQuery
vue-cli与webpack处理静态资源的方法及webpack打包的坑
2018/05/15 Javascript
react native 文字轮播的实现示例
2018/07/27 Javascript
小程序云开发实战小结
2018/10/25 Javascript
Element中的Cascader(级联列表)动态加载省\市\区数据的方法
2019/03/27 Javascript
vue实现自定义H5视频播放器的方法步骤
2019/07/01 Javascript
微信小程序如何利用getCurrentPages进行页面传值
2019/07/01 Javascript
[11:33]DAC2018 4.5SOLO赛决赛 MidOne vs Paparazi第二场
2018/04/06 DOTA
python cx_Oracle模块的安装和使用详细介绍
2017/02/13 Python
Python语言的变量认识及操作方法
2018/02/11 Python
django最快程序开发流程详解
2019/07/19 Python
Python:slice与indices的用法
2019/11/25 Python
python读取图像矩阵文件并转换为向量实例
2020/06/18 Python
Python pytesseract验证码识别库用法解析
2020/06/29 Python
css3个性化字体_动力节点Java学院整理
2017/07/12 HTML / CSS
使用HTML5 Canvas绘制直线或折线等线条的方法讲解
2016/03/14 HTML / CSS
纽约21世纪百货官网:Century 21
2016/08/27 全球购物
英国最大的在线快递公司之一:ParcelHero
2019/11/04 全球购物
澳大利亚电商Catch新西兰站:Catch.co.nz
2020/05/30 全球购物
生产车间实习自我鉴定
2013/09/23 职场文书
导购员的岗位职责
2014/02/08 职场文书
个人委托书
2014/07/31 职场文书
乡镇组织委员个人整改措施
2014/09/16 职场文书
优质护理心得体会
2016/01/22 职场文书
学习心理学心得体会
2016/01/22 职场文书
原生Js 实现的简单无缝滚动轮播图的示例代码
2021/05/10 Javascript
Nginx利用Logrotate实现日志分割
2022/05/20 Servers
MySQL导致索引失效的几种情况
2022/06/25 MySQL