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的Tornado框架中实现简单的在线代理的教程
May 02 Python
Python 专题二 条件语句和循环语句的基础知识
Mar 19 Python
python3 遍历删除特定后缀名文件的方法
Apr 23 Python
使用Selenium破解新浪微博的四宫格验证码
Oct 19 Python
Python地图绘制实操详解
Mar 04 Python
Django高级编程之自定义Field实现多语言
Jul 02 Python
Python实现企业微信机器人每天定时发消息实例
Feb 25 Python
在pytorch中实现只让指定变量向后传播梯度
Feb 29 Python
借助Paramiko通过Python实现linux远程登陆及sftp的操作
Mar 16 Python
python语言中有算法吗
Jun 16 Python
详解Django中的FBV和CBV对比分析
Mar 01 Python
Python编程super应用场景及示例解析
Oct 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脚本不报错的原因
2014/06/12 PHP
PHP获取网页所有连接的方法(附demo源码下载)
2016/03/30 PHP
微信公众号开发之语音消息识别php代码
2016/08/08 PHP
PHP+MySQL高并发加锁事务处理问题解决方法
2018/04/30 PHP
jQuery 瀑布流 绝对定位布局(二)(延迟AJAX加载图片)
2012/05/23 Javascript
使用indexOf等在JavaScript的数组中进行元素查找和替换
2013/09/18 Javascript
javascript验证上传文件的类型限制必须为某些格式
2013/11/14 Javascript
jquery制作居中遮罩层效果分享
2014/02/21 Javascript
优化Node.js Web应用运行速度的10个技巧
2014/09/03 Javascript
jQuery制作可自定义大小的拼图游戏
2015/03/30 Javascript
JavaScript核心语法总结(推荐)
2016/06/02 Javascript
基于vuejs+webpack的日期选择插件
2020/05/21 Javascript
利用node.js实现自动生成前端项目组件的方法详解
2017/07/12 Javascript
微信小程序注册60s倒计时功能 使用JS实现注册60s倒计时功能
2017/08/16 Javascript
vue实现在一个方法执行完后执行另一个方法的示例
2018/08/25 Javascript
详解Vue CLI 3.0脚手架如何mock数据
2018/11/23 Javascript
详解JavaScript 为什么要有 Symbol 类型?
2019/04/03 Javascript
细说Vue组件的服务器端渲染的过程
2019/05/30 Javascript
微信小程序实现同时上传多张图片
2020/02/03 Javascript
微信小程序点击按钮动态切换input的disabled禁用/启用状态功能
2020/03/07 Javascript
python爬取各类文档方法归类汇总
2018/03/22 Python
python 获取键盘输入,同时有超时的功能示例
2018/11/13 Python
PyQt5根据控件Id获取控件对象的方法
2019/06/25 Python
PyQt 图解Qt Designer工具的使用方法
2019/08/06 Python
python语言线程标准库threading.local解读总结
2019/11/10 Python
基于打开pycharm有带图片md文件卡死问题的解决
2020/04/24 Python
Pandas实现一列数据分隔为两列
2020/05/18 Python
澳大利亚吉他在线:Artist Guitars
2017/03/30 全球购物
中学生差生评语
2014/01/30 职场文书
土地转让协议书
2014/04/15 职场文书
夫妻双方自愿离婚协议书
2014/10/24 职场文书
2015年大学生工作总结
2015/04/21 职场文书
让世界充满爱观后感
2015/06/10 职场文书
2015年六年级班主任工作总结
2015/10/15 职场文书
Python查找算法的实现 (线性、二分,分块、插值查找算法)
2022/04/24 Python
Mysql中常用的join连接方式
2022/05/11 MySQL