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 简易计算器程序,代码就几行
Aug 29 Python
Python版的文曲星猜数字游戏代码
Sep 02 Python
Python程序员鲜为人知但你应该知道的17个问题
Jun 04 Python
Python数据分析之真实IP请求Pandas详解
Nov 18 Python
Python实现将一个大文件按段落分隔为多个小文件的简单操作方法
Apr 17 Python
CentOS下使用yum安装python-pip失败的完美解决方法
Aug 16 Python
Python实现小数转化为百分数的格式化输出方法示例
Sep 20 Python
python在线编译器的简单原理及简单实现代码
Feb 02 Python
解决Python中list里的中文输出到html模板里的问题
Dec 17 Python
Python中函数参数匹配模型详解
Jun 09 Python
keras在构建LSTM模型时对变长序列的处理操作
Jun 29 Python
发工资啦!教你用Python实现邮箱自动群发工资条
May 10 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
dedecms中常见问题修改方法总结
2007/03/21 PHP
PHP中获取变量的变量名的一段代码的bug分析
2011/07/07 PHP
codeigniter中view通过循环显示数组数据的方法
2015/03/20 PHP
培养自己的php编码规范
2015/09/28 PHP
PHP检查网站是否宕机的方法示例
2017/07/24 PHP
jQuery表格排序组件-tablesorter使用示例
2014/05/26 Javascript
使用jQuery判断IE浏览器版本的代码
2014/06/14 Javascript
一个仿糯米弹框效果demo
2014/07/22 Javascript
Jquery对新插入的节点 绑定Click事件失效的解决方法
2016/06/02 Javascript
JS使用正则表达式过滤多个词语并替换为相同长度星号的方法
2016/08/03 Javascript
json定义及jquery操作json的方法
2016/09/29 Javascript
javascript 正则表达式去空行方法
2017/01/24 Javascript
实例讲解Vue.js中router传参
2018/04/22 Javascript
小程序点赞收藏功能的实现代码示例
2018/09/07 Javascript
vue中组件的3种使用方式详解
2019/03/23 Javascript
vue控制多行文字展开收起的实现示例
2019/10/11 Javascript
Vue使用Ref跨层级获取组件的步骤
2021/01/25 Vue.js
python实现在sqlite动态创建表的方法
2015/05/08 Python
Python使用matplotlib绘制动画的方法
2015/05/20 Python
Python用 KNN 进行验证码识别的实现方法
2018/02/06 Python
Python面向对象程序设计之类的定义与继承简单示例
2019/03/18 Python
深入了解如何基于Python读写Kafka
2019/12/31 Python
tensorflow实现对张量数据的切片操作方式
2020/01/19 Python
解决TensorFlow GPU版出现OOM错误的问题
2020/02/03 Python
详解CSS3媒体查询响应式布局bootstrap 框架原理实战(推荐)
2020/11/16 HTML / CSS
SmartBuyGlasses荷兰:购买太阳镜和眼镜
2020/03/16 全球购物
String是最基本的数据类型吗?
2013/06/13 面试题
网络公司美工设计工作个人的自我评价
2013/11/03 职场文书
电大毕业自我鉴定
2014/02/03 职场文书
质检部经理岗位职责
2014/02/19 职场文书
中层干部竞聘演讲稿
2014/05/15 职场文书
本科生就业推荐信
2014/05/19 职场文书
2014全年工作总结
2014/11/27 职场文书
2014个人年度工作总结
2014/12/15 职场文书
详解python网络进程
2021/06/15 Python
php访问对象中的成员的实例方法
2021/11/17 PHP