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实现的批量下载RFC文档
Mar 10 Python
Python中为什么要用self探讨
Apr 14 Python
Python实现的Google IP 可用性检测脚本
Apr 23 Python
关于Python元祖,列表,字典,集合的比较
Jan 06 Python
如何用Python合并lmdb文件
Jul 02 Python
python3+requests接口自动化session操作方法
Oct 13 Python
python射线法判断检测点是否位于区域外接矩形内
Jun 28 Python
python查找重复图片并删除(图片去重)
Jul 16 Python
win10下python2和python3共存问题解决方法
Dec 23 Python
python不使用for计算两组、多个矩形两两间的iou方式
Jan 18 Python
Python中无限循环需要什么条件
May 27 Python
Python 中的单分派泛函数你真的了解吗
Jun 22 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
骨王战斗力在公会成员中排不进前五,却当选了会长,原因竟是这样
2020/03/02 日漫
PHP多线程抓取网页实现代码
2010/07/22 PHP
php之Smarty模板使用方法示例详解
2014/07/08 PHP
php生成4位数字验证码的实现代码
2015/11/23 PHP
PHP 等比例缩放图片详解及实例代码
2016/09/18 PHP
PHP编程中的Session阻塞问题与解决方法分析
2017/08/07 PHP
浅谈Laravel队列实现原理解决问题记录
2017/08/19 PHP
php适配器模式简单应用示例
2019/10/23 PHP
用JavaScript 处理 URL 的两个函数代码
2007/08/13 Javascript
动态显示可输入的字数提示还可以输入的字数
2014/04/01 Javascript
javascript解决IE6下hover问题的方法
2015/07/28 Javascript
JavaScript使用FileSystemObject对象写入文本文件内容的方法
2015/08/05 Javascript
使用JavaScript实现弹出层效果的简单实例
2016/05/31 Javascript
详解使用JS如何制作简单的ASCII图与单极图
2017/03/31 Javascript
bootstrap table表格插件使用详解
2017/05/08 Javascript
解决JQuery全选/反选第二次失效的问题
2017/10/11 jQuery
vue watch深度监听对象实现数据联动效果
2018/08/16 Javascript
inquirer.js一个用户与命令行交互的工具详解
2019/05/18 Javascript
Node.js系列之安装配置与基本使用(1)
2019/08/30 Javascript
layer.open 子页面弹出层向父页面传输数据的例子
2019/09/26 Javascript
[05:06]TI4西雅图DOTA2前线报道 海涛密探LGD训练
2014/07/09 DOTA
Python下实现的RSA加密/解密及签名/验证功能示例
2017/07/17 Python
详解python opencv、scikit-image和PIL图像处理库比较
2019/12/26 Python
基于CSS3的animation属性实现微信拍一拍动画效果
2020/06/22 HTML / CSS
html5调用摄像头功能的实现代码
2018/05/07 HTML / CSS
详解HTML5 data-* 自定义属性
2018/01/24 HTML / CSS
解决HTML5中滚动到底部的事件问题
2019/08/22 HTML / CSS
AmazeUI 网格的实现示例
2020/08/13 HTML / CSS
澳大利亚领先的时尚内衣零售商:Bras N Things
2020/07/28 全球购物
北大自主招生自荐信
2013/10/19 职场文书
物流司机岗位职责
2013/12/28 职场文书
义和团口号
2014/06/17 职场文书
离婚协议书格式
2014/11/21 职场文书
幼儿园小班开学寄语(2016秋季)
2015/12/03 职场文书
Vue详细的入门笔记
2021/05/10 Vue.js
Vue组件化(ref,props, mixin,.插件)详解
2022/05/15 Vue.js