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使用(完全图解(最新经典))
随机推荐
- php遍历路径——php经典实例
php遍历路径——php经典实例 代码: <html> <head> <title>遍历目录</title> <meta charset=&quo ...
- thinkphp中的验证器
- python使用requests模块模拟登陆知乎
from bs4 import BeautifulSoup import requests import time def captcha(captcha_data): with open(" ...
- [node.js] async/await如何优雅处理异常?
node.js的世界,从callback开始,不会止于async. 所有人都在骂为什么不能完全进化,其实我感觉这就是老外的细心,为了承上.这也就是为什么async其实就是promise一样,假如不是一 ...
- 20165301 2017-2018-2 《Java程序设计》第七周学习总结
20165301 2017-2018-2 <Java程序设计>第七周学习总结 教材学习内容总结 第十一章:JDBC与MySQL数据库 MySQL数据库管理系统 启动MySQL数据库服务器 ...
- Zabbix定义报警机制
1. 修改zabbix配置文件 #取消注释或添加一行 cat -n /etc/zabbix/zabbix_server.conf |grep --color=auto "AlertScrip ...
- 进程自我保护 适用于WIN7 X64
//进程自我保护,注意只有X64 WIN7可用 #include <ntddk.h> #define PROCESS_TERMINATE 1 typedef struct _LDR_DAT ...
- CentOS6.9下安装MariaDB10.2.11
yum groupinstall -y "Development Tools" yum install -y cmake openssl-devel zlib-devel yum ...
- Three.js基础探寻八——法向材质与材质的纹理贴图
4.法向材质 法向材质可以将材质的颜色设置为其法向量的方向,有时候对于调试很有帮助. 法向材质的设定很简单,甚至不用设置任何参数: new THREE.MeshNormalMaterial() 材质的 ...
- jquery validate表单验证插件的基本使用方法及功能拓展
1 表单验证的准备工作 在开启长篇大论之前,首先将表单验证的效果展示给大家. 1.点击表单项,显示帮助提示 2.鼠标离开表单项时,开始校验元素 3.鼠标离开后的正确.错误提示及鼠标移入时的帮助提 ...