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判断值是否在list或set中的性能对比分析
Apr 16 Python
python hook监听事件详解
Oct 25 Python
pandas 选取行和列数据的方法详解
Aug 08 Python
Python一键安装全部依赖包的方法
Aug 12 Python
Python实现某论坛自动签到功能
Aug 20 Python
Python模拟登录之滑块验证码的破解(实例代码)
Nov 18 Python
使用python动态生成波形曲线的实现
Dec 04 Python
python+tifffile之tiff文件读写方式
Jan 13 Python
利用python在excel中画图的实现方法
Mar 17 Python
Python grpc超时机制代码示例
Sep 14 Python
如何在Python3中使用telnetlib模块连接网络设备
Sep 21 Python
2021年最新用于图像处理的Python库总结
Jun 15 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 转换字符串编码 iconv与mb_convert_encoding的区别说明
2011/11/10 PHP
php使用ICQ网关发送手机短信
2013/10/30 PHP
PHP生成不重复随机数的方法汇总
2014/11/19 PHP
PHP中使用array函数新建一个数组
2015/11/19 PHP
PHP利用Cookie设置用户30分钟未操作自动退出功能
2017/07/03 PHP
分析php://output和php://stdout的区别
2018/05/06 PHP
PHP格式化显示时间date()函数代码
2018/10/03 PHP
PHP通过文件保存和更新信息的方法分析
2019/09/12 PHP
限制复选框的最大可选数
2006/07/01 Javascript
[转]JS宝典学习笔记
2007/02/07 Javascript
JavaScript 不只是脚本
2007/05/30 Javascript
jquery下操作HTML控件的实现代码
2010/01/12 Javascript
JS实现侧悬浮浮动实例代码
2013/11/29 Javascript
JavaScript中字面量与函数的基本使用知识
2015/10/20 Javascript
基于Vuejs框架实现翻页组件
2020/06/29 Javascript
NodeJS配置HTTPS服务实例分享
2017/02/19 NodeJs
jQuery实现的淡入淡出图片轮播效果示例
2018/08/29 jQuery
服务端预渲染之Nuxt(使用篇)
2019/04/08 Javascript
详解在HTTPS 项目中使用百度地图 API
2019/04/26 Javascript
[49:08]FNATIC vs Infamous 2019国际邀请赛小组赛 BO2 第二场 8.16
2019/08/18 DOTA
使用python绘制人人网好友关系图示例
2014/04/01 Python
使用Python下的XSLT API进行web开发的简单教程
2015/04/15 Python
用python做一个搜索引擎(Pylucene)的实例代码
2017/07/05 Python
解决python删除文件的权限错误问题
2018/04/24 Python
Python txt文件加入字典并查询的方法
2019/01/15 Python
详解用pyecharts Geo实现动态数据热力图城市找不到问题解决
2019/06/26 Python
Django 源码WSGI剖析过程详解
2019/08/05 Python
Python语言异常处理测试过程解析
2020/01/08 Python
python批量提取图片信息并保存的实现
2021/02/05 Python
英语教学随笔感言
2014/02/20 职场文书
祖国在我心中演讲稿500字
2014/05/04 职场文书
幼儿发展评估方案
2014/06/11 职场文书
科长个人四风问题整改措施思想汇报
2014/10/13 职场文书
挂职锻炼个人总结
2015/03/05 职场文书
孟佩杰观后感
2015/06/17 职场文书
入队仪式主持词
2015/07/04 职场文书