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爬虫之爬虫编写全记录
Nov 06 Python
Python实现批量读取word中表格信息的方法
Jul 30 Python
Python3实现发送QQ邮件功能(文本)
Dec 15 Python
Python3爬虫学习之爬虫利器Beautiful Soup用法分析
Dec 12 Python
python批量获取html内body内容的实例
Jan 02 Python
python爬虫的一个常见简单js反爬详解
Jul 09 Python
python爬虫之爬取百度音乐的实现方法
Aug 24 Python
python 模拟贷款卡号生成规则过程解析
Aug 30 Python
解决python父线程关闭后子线程不关闭问题
Apr 25 Python
Tensorflow实现将标签变为one-hot形式
May 22 Python
selenium判断元素是否存在的两种方法小结
Dec 07 Python
matplotlib之pyplot模块之标题(title()和suptitle())
Feb 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
如何对PHP程序中的常见漏洞进行攻击
2006/10/09 PHP
PHP Zip压缩 在线对文件进行压缩的函数
2010/05/26 PHP
php+ajax做仿百度搜索下拉自动提示框(有实例)
2012/08/21 PHP
基于PHP输出缓存(output_buffering)的深入理解
2013/06/13 PHP
PHP排序二叉树基本功能实现方法示例
2018/05/26 PHP
PHP中localeconv()函数的用法
2019/03/26 PHP
Laravel5.7 数据库操作迁移的实现方法
2019/04/12 PHP
扩展IE中一些不兼容的方法如contains、startWith等等
2014/01/09 Javascript
JQuery控制radio选中和不选中方法总结
2015/04/15 Javascript
jQuery实现网站添加高亮突出显示效果的方法
2015/06/26 Javascript
让DIV的滚动条自动滚动到最底部的3种方法(推荐)
2016/09/24 Javascript
ES6新特征数字、数组、字符串
2016/10/01 Javascript
Jquery鼠标放上去显示全名的实现方法
2017/02/06 Javascript
vue项目中v-model父子组件通信的实现详解
2017/12/10 Javascript
Angular2进阶之如何避免Dom误区
2018/04/02 Javascript
讲解vue-router之命名路由和命名视图
2018/05/28 Javascript
微信小程序实现复选框效果
2018/12/28 Javascript
js prototype深入理解及应用实例分析
2019/11/25 Javascript
nodejs开发一个最简单的web服务器实例讲解
2020/01/02 NodeJs
Vue组件间的通信pubsub-js实现步骤解析
2020/03/11 Javascript
Python制作简单的网页爬虫
2015/11/22 Python
python 读写中文json的实例详解
2017/10/29 Python
给你选择Python语言实现机器学习算法的三大理由
2017/11/15 Python
微信跳一跳python辅助脚本(总结)
2018/01/11 Python
Python实现FLV视频拼接功能
2020/01/21 Python
Keras:Unet网络实现多类语义分割方式
2020/06/11 Python
你不知道的葡萄干处理法、橙蜜处理法、二氧化碳酵母法
2021/03/17 冲泡冲煮
表达自我的市场:Society6
2018/08/01 全球购物
酷瑞网络科技面试题
2012/03/30 面试题
《燕子专列》教学反思
2014/02/21 职场文书
政风行风评议整改方案
2014/09/15 职场文书
四风问题班子对照检查材料
2014/09/27 职场文书
2014年客服工作总结与计划
2014/12/09 职场文书
2015年学校远程教育工作总结
2015/07/20 职场文书
2019通用版新员工入职培训方案!
2019/07/11 职场文书
Mysql数据库group by原理详解
2022/07/07 MySQL