• linkedu视频
  • 平面设计
  • 电脑入门
  • 操作系统
  • 办公应用
  • 电脑硬件
  • 动画设计
  • 3D设计
  • 网页设计
  • CAD设计
  • 影音处理
  • 数据库
  • 程序设计
  • 认证考试
  • 信息管理
  • 信息安全
菜单
linkedu.com
  • 网页制作
  • 数据库
  • 程序设计
  • 操作系统
  • CMS教程
  • 游戏攻略
  • 脚本语言
  • 平面设计
  • 软件教程
  • 网络安全
  • 电脑知识
  • 服务器
  • 视频教程
  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell
您的位置:首页 > 脚本语言 >python > python统计文本字符串里单词出现频率的方法

python统计文本字符串里单词出现频率的方法

作者: 字体:[增加 减小] 来源:互联网

通过本文主要向大家介绍了python字符串处理,python 列表转字符串,python 字符串,python 字符串转数字,python 字符串截取等相关知识,希望对您有所帮助,也希望大家支持linkedu.com www.linkedu.com

本文实例讲述了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])

</div>

希望本文所述对大家的Python程序设计有所帮助。

</div>

您可能想查找下面的文章:

  • Python 专题三 字符串的基础知识
  • python 截取 取出一部分的字符串方法
  • Python实现字符串格式化的方法小结
  • python数据清洗系列之字符串处理详解
  • Python 基础知识之字符串处理
  • Python 基础知识之字符串处理
  • Python中字符串的修改及传参详解
  • python中异常报错处理方法汇总
  • 简单谈谈Python中的反转字符串问题
  • Python中字符串的处理技巧分享

相关文章

  • Python算法应用实战之队列详解
  • 利用Python抓取行政区划码的方法
  • Python中pip安装非PyPI官网第三方库的方法
  • 用python分割TXT文件成4K的TXT文件
  • python实现从ftp服务器下载文件的方法
  • python 提取文件的小程序
  • Flask框架中密码的加盐哈希加密和验证功能的用法详解
  • python模拟登录百度贴吧(百度贴吧登录)实例
  • Python中的yield浅析
  • Python实现代码统计工具(终极篇)

文章分类

  • vbs
  • DOS/BAT
  • hta/htc
  • python
  • perl
  • VBA
  • ColdFusion
  • ruby
  • PowerShell
  • Lua
  • Golang
  • linux shell

最近更新的内容

    • python写xml文件的操作实例
    • Python多线程学习资料
    • Python字符串替换实例分析
    • Python中使用MELIAE分析程序内存占用实例
    • python定时器使用示例分享
    • python xml解析实例详解
    • Python写的一个定时重跑获取数据库数据
    • python操作数据库之sqlite3打开数据库、删除、修改示例
    • Django框架中的对象列表视图使用示例
    • Python实现处理管道的方法

关于我们 - 联系我们 - 免责声明 - 网站地图

©2020-2025 All Rights Reserved. linkedu.com 版权所有