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:socket传输大文件示例
Jan 18 Python
pygame实现弹力球及其变速效果
Jul 03 Python
Python实现矩阵加法和乘法的方法分析
Dec 19 Python
python os用法总结
Jun 08 Python
Django学习笔记之为Model添加Action
Apr 30 Python
详解Python列表赋值复制深拷贝及5种浅拷贝
May 15 Python
利用Python制作动态排名图的实现代码
Apr 09 Python
解决Jupyter notebook中.py与.ipynb文件的import问题
Apr 21 Python
Pycharm如何导入python文件及解决报错问题
May 10 Python
深入了解Python装饰器的高级用法
Aug 13 Python
python飞机大战游戏实例讲解
Dec 04 Python
pycharm进入时每次都是insert模式的解决方式
Feb 05 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中return的用法实例分析
2015/02/28 PHP
PHP简单实现数字分页功能示例
2016/08/24 PHP
ThinkPHP中create()方法自动验证表单信息
2017/04/28 PHP
有关PHP 中 config.m4 的探索
2020/08/26 PHP
利用jQuery接受和处理xml数据的代码(.net)
2011/03/28 Javascript
jquery获取颜色在ie和ff下的区别示例介绍
2014/03/28 Javascript
jQuery实现表格行上移下移和置顶的方法
2015/05/22 Javascript
再次谈论Javascript中的this
2016/06/23 Javascript
BootStrap日期控件在模态框中选择时间下拉菜单无效的原因及解决办法(火狐下不能点击)
2016/08/18 Javascript
JavaScript实现url参数转成json形式
2016/09/25 Javascript
Jquery Easyui自定义下拉框组件使用详解(21)
2020/12/31 Javascript
浅谈react.js中实现tab吸顶效果的问题
2017/09/06 Javascript
Angularjs渲染的 using 指令的星级评分系统示例
2017/11/09 Javascript
微信小程序实现跟随菜单效果和循环嵌套加载数据
2017/11/21 Javascript
vue elementui form表单验证的实现
2018/11/11 Javascript
JS使用setInterval计时器实现挑战10秒
2020/11/08 Javascript
[02:57]2014DOTA2国际邀请赛-观众采访
2014/07/19 DOTA
详解Python中break语句的用法
2015/05/14 Python
Python中逗号的三种作用实例分析
2015/06/08 Python
对Python进行数据分析_关于Package的安装问题
2017/05/22 Python
PyQt实现界面翻转切换效果
2018/04/20 Python
python实现五子棋人机对战游戏
2020/03/25 Python
python模块和包的应用BASE_PATH使用解析
2019/12/14 Python
Python实现bilibili时间长度查询的示例代码
2020/01/14 Python
python根据字典的键来删除元素的方法
2020/08/16 Python
HTML5 本地存储 LocalStorage详解
2016/06/24 HTML / CSS
Reebonz中国官网:新加坡奢侈品购物网站
2017/03/17 全球购物
Guess欧洲官网:美国服饰品牌
2019/08/06 全球购物
成功的酒店创业计划书
2013/12/27 职场文书
高三地理教学反思
2014/01/11 职场文书
大学专科求职信
2014/07/02 职场文书
社会体育专业大学生职业生涯规划书
2014/09/17 职场文书
2014小学生国庆65周年演讲稿
2014/09/21 职场文书
出资证明书范本(标准版)
2014/09/24 职场文书
python for循环赋值问题
2021/06/03 Python
一次Mysql update sql不当引起的生产故障记录
2022/04/01 MySQL