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获取指定路径下所有指定后缀文件的方法
May 26 Python
python 多线程实现检测服务器在线情况
Nov 25 Python
pyspark 读取csv文件创建DataFrame的两种方法
Jun 07 Python
浅谈python requests 的put, post 请求参数的问题
Jan 02 Python
使用Python操作FTP实现上传和下载的方法
Apr 01 Python
Python小白必备的8个最常用的内置函数(推荐)
Apr 03 Python
Python PO设计模式的具体使用
Aug 16 Python
Python处理session的方法整理
Aug 29 Python
基于Python实现船舶的MMSI的获取(推荐)
Oct 21 Python
Python简易计算器制作方法代码详解
Oct 31 Python
python 模拟登陆163邮箱
Dec 15 Python
Pytorch中TensorBoard及torchsummary的使用详解
May 12 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 高级课程笔记 面向对象
2009/06/21 PHP
PHP微信开发用Cache 解决数据缓存
2016/07/11 PHP
PHP全局使用Laravel辅助函数dd
2019/12/26 PHP
PHP语言对接抖音快手小红书视频/图片去水印API接口源码
2020/08/11 PHP
分享几种好用的PHP自定义加密函数(可逆/不可逆)
2020/09/15 PHP
javascript document.referrer 用法
2009/04/30 Javascript
js通过googleAIP翻译PHP系统的语言配置的实现代码
2011/10/17 Javascript
在javascript中关于节点内容加强
2013/04/11 Javascript
深入分析JQuery和JavaScript的异同
2014/10/23 Javascript
基于Vuejs框架实现翻页组件
2020/06/29 Javascript
利用types增强vscode中js代码提示功能详解
2017/07/07 Javascript
详解如何构建Angular项目目录结构
2017/07/13 Javascript
JavaScript数据结构与算法之基本排序算法定义与效率比较【冒泡、选择、插入排序】
2019/02/21 Javascript
Vue跨域请求问题解决方案过程解析
2020/08/07 Javascript
python 设置文件编码格式的实现方法
2017/12/21 Python
Python+request+unittest实现接口测试框架集成实例
2018/03/16 Python
解读python logging模块的使用方法
2018/04/17 Python
浅谈tensorflow1.0 池化层(pooling)和全连接层(dense)
2018/04/27 Python
对python中xlsx,csv以及json文件的相互转化方法详解
2018/12/25 Python
python如何爬取网站数据并进行数据可视化
2019/07/08 Python
对Python获取屏幕截图的4种方法详解
2019/08/27 Python
python实现代码统计器
2019/09/19 Python
django2.2安装错误最全的解决方案(小结)
2019/09/24 Python
HTML5 canvas标签实现刮刮卡效果
2015/04/24 HTML / CSS
印尼值得信赖的在线交易网站:Bukalapak
2019/03/11 全球购物
你们项目是如何进行变更控制的
2015/08/26 面试题
行政部工作岗位职责范本
2014/03/05 职场文书
小学三八妇女节活动方案
2014/03/16 职场文书
校园文明倡议书
2014/05/16 职场文书
消防安全宣传口号
2014/06/10 职场文书
元旦晚会活动总结
2014/07/09 职场文书
党的群众路线教育实践活动对照检查材料(教师)
2014/09/24 职场文书
工作失误检讨书(3篇)
2014/10/11 职场文书
圣诞晚会主持词
2015/07/01 职场文书
民事纠纷协议书
2016/03/23 职场文书
HTML基本元素标签介绍
2022/02/28 HTML / CSS