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命令行参数sys.argv使用示例
Jan 28 Python
python批量生成本地ip地址的方法
Mar 23 Python
Python中用Spark模块的使用教程
Apr 13 Python
Python and、or以及and-or语法总结
Apr 14 Python
Python自动登录126邮箱的方法
Jul 10 Python
Pandas 同元素多列去重的实例
Jul 03 Python
python3基于OpenCV实现证件照背景替换
Jul 18 Python
详解Python中的内建函数,可迭代对象,迭代器
Apr 29 Python
python实现最大子序和(分治+动态规划)
Jul 05 Python
详解Python文件修改的两种方式
Aug 22 Python
Python 解决火狐浏览器不弹出下载框直接下载的问题
Mar 09 Python
Pytorch反向传播中的细节-计算梯度时的默认累加操作
Jun 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设计模式 Command(命令模式)
2011/06/26 PHP
PHP表单验证内容是否为空的实现代码
2016/11/14 PHP
一键生成各种尺寸Icon的php脚本(实例)
2017/02/08 PHP
PHP操作Postgresql封装类与应用完整实例
2018/04/24 PHP
php操作mongodb封装类与用法实例
2018/09/01 PHP
javascript void(0)的妙用
2009/10/21 Javascript
Jquery Ajax 学习实例2 向页面发出请求 返回JSon格式数据
2010/03/15 Javascript
jQuery对表单元素的取值和赋值操作代码
2011/05/19 Javascript
JavaScript SetInterval与setTimeout使用方法详解
2013/11/15 Javascript
获取当前点击按钮的id用this.id实现
2014/03/17 Javascript
JavaScript判断用户是否对表单进行了修改的方法
2015/03/18 Javascript
JQuery的attr 与 val区别
2016/06/12 Javascript
JS中使用DOM来控制HTML元素
2016/07/31 Javascript
浅谈javascript控制HTML5的全屏操控,浏览器兼容的问题
2016/10/10 Javascript
jQuery生成假加载动画效果
2016/12/01 Javascript
详解JavaScript RegExp对象
2017/02/04 Javascript
12条写出高质量JS代码的方法
2018/01/07 Javascript
Node.js readline模块与util模块的使用
2018/03/01 Javascript
利用Node.js批量抓取高清妹子图片实例教程
2018/08/02 Javascript
基于JS实现简单滑块拼图游戏
2019/10/12 Javascript
vue组件暴露和.js文件暴露接口操作
2020/08/11 Javascript
python发布模块的步骤分享
2014/02/21 Python
Python实现端口复用实例代码
2014/07/03 Python
使用Scrapy爬取动态数据
2018/10/21 Python
Python实现点阵字体读取与转换的方法
2019/01/29 Python
python3实现高效的端口扫描
2019/08/31 Python
pandas apply使用多列计算生成新的列实现示例
2021/02/24 Python
CSS3中的transform属性进行2D和3D变换的基本用法
2016/05/12 HTML / CSS
html5指南-4.使用Geolocation实现定位功能
2013/01/07 HTML / CSS
John Hardy官方网站:手工设计首饰的奢侈品牌
2017/07/05 全球购物
奥斯汀独木舟和皮划艇:Austin Canoe & Kayak
2018/05/22 全球购物
一年级语文下册复习计划
2015/01/17 职场文书
2015年度招聘工作总结
2015/05/28 职场文书
课改心得体会范文
2016/01/25 职场文书
医学会议开幕词
2016/03/03 职场文书
学校就业保障协议书
2019/06/24 职场文书