python开发_re和counter
python中re和counter的结合,可以实现以下的功能:
1.获取字符串或者文件中的单词组
2.对单词组进行统计
下面是我做的demo
运行效果:

=============================================
代码部分:
=============================================
#python re and counter object
'''
读取一个文件,获取到该文件中的所有单词组,然后对该单词组进行个数统计,也可以根据
条件统计,如:该单词组中出现最多的前number个单词
'''
import os
import re
from collections import Counter def get_words(path):
'''读取一个文件中的内容,返回该文件中的所有单词'''
if os.path.exists(path):
return re.findall(r'\w+', open(path).read().lower())
else:
print('the path [{}] is not exist!'.format(path)) def get_most_common_words(words, number):
'''
如果<code>number > 0</code>,则返回该单词组中出现最多的前<code>number</code>个单词
否则,返回该单词组中所有统计情况
'''
if number > 0:
return Counter(words).most_common(number)
else:
return Counter(words) def main():
temp_path = 'c:\\temp.txt'
number = 5
words = get_words(temp_path)
print(words)
print('#' * 50)
cnt = get_most_common_words(words, -1)
print(cnt)
print('#' * 50)
cnt = get_most_common_words(words, number)
print(cnt) if __name__ == '__main__':
main()
python开发_re和counter的更多相关文章
- python开发学习-day01 (python安装与版本、字符串、字典、运算符、文件)
*:first-child { margin-top: 0 !important; } body>*:last-child { margin-bottom: 0 !important; } /* ...
- Python开发【第二十一篇】:Web框架之Django【基础】
Python开发[第二十一篇]:Web框架之Django[基础] 猛击这里:http://www.cnblogs.com/wupeiqi/articles/5237704.html Python之 ...
- python开发_python关键字
python3.3.2中的关键字如下: The following identifiers are used as reserved words, or keywords of the languag ...
- python开发环境搭建
虽然网上有很多python开发环境搭建的文章,不过重复造轮子还是要的,记录一下过程,方便自己以后配置,也方便正在学习中的同事配置他们的环境. 1.准备好安装包 1)上python官网下载python运 ...
- 【Machine Learning】Python开发工具:Anaconda+Sublime
Python开发工具:Anaconda+Sublime 作者:白宁超 2016年12月23日21:24:51 摘要:随着机器学习和深度学习的热潮,各种图书层出不穷.然而多数是基础理论知识介绍,缺乏实现 ...
- Python开发工具PyCharm个性化设置(图解)
Python开发工具PyCharm个性化设置,包括设置默认PyCharm解析器.设置缩进符为制表符.设置IDE皮肤主题等,大家参考使用吧. JetBrains PyCharm Pro 4.5.3 中文 ...
- Python黑帽编程1.2 基于VS Code构建Python开发环境
Python黑帽编程1.2 基于VS Code构建Python开发环境 0.1 本系列教程说明 本系列教程,采用的大纲母本为<Understanding Network Hacks Atta ...
- Eclipse中Python开发环境搭建
Eclipse中Python开发环境搭建 目 录 1.背景介绍 2.Python安装 3.插件PyDev安装 4.测试Demo演示 一.背景介绍 Eclipse是一款基于Java的可扩展开发平台. ...
- Python开发:环境搭建(python3、PyCharm)
Python开发:环境搭建(python3.PyCharm) python3版本安装 PyCharm使用(完全图解(最新经典))
随机推荐
- 大图片上传(ImageIO,注意有的图片不能上传时因为他是tiff格式)
一下是必要的: 1.enctype="multipart/form-data" 2. //不要使用myeclipse自动生成的get.set方法(struts2中的用法) publ ...
- oracle imp dmp命令
vi par.txt userid=system/oracle tables=(user.table,...) query="where org_no like 32%" file ...
- 做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table【转】
做Mysql主从时,注意使用replicate_wild_do_table和replicate-wild-ignore-table 浓缩版: 使用replicate_do_db和replicate_i ...
- 004ICMP-type对应表
一次在某个防火墙配置策略里看到如下的代码: iptables -A INPUT -p icmp --icmp-type 8 -j ACCEPT iptables -A FORWARD -p icmp ...
- myeclipse/eclipse安装反编译插件jadclipse
jad是一个使用比较广泛的Java反编译软件,jadClipse是jad在eclipse下的插件,下面像大家介绍下如何将jadclipse加入到myeclipse/eclipse中. 文件下载 (1) ...
- 模板为webpack的目录结构
目录结构 | -- build // 项目构建(webpack)相关代码 | |-- build.js // 生产环境构建代码 | |-- check-version.js // 检查node.npm ...
- Effective C++笔记(六):继承与面向对象设计
参考:http://www.cnblogs.com/ronny/p/3756494.html 条款32:确定你的public继承塑模出is-a关系 “public继承”意味着is-a.适用于base ...
- curd 插件
1. Django项目启动 自动加载文件 制作启动文件 . 注册strak 在apps.py 类里面增加如下 def ready(self): from django.utils.module_loa ...
- 观察者模式和java委托
观察者模式与java委托 所谓观察者模式,指的某个状态信息的改变,会影响其他一系列的操作,这时就可以将这些操作抽象化,同时创建一个类统一的管理和执行这些操作.把这些抽象出来的操作称为观察者类,而管理这 ...
- Adding Completion to (interactive)
Adding Completion to (interactive) Author: Tubo Question: Is there any way to add my own completio ...