python统计字词练习
方法一:
import operator
from nltk.corpus import stopwords
stop_words = stopwords.words('English')#目的是去除人称代词等,注意根据编译提示下载相应库 speech_text = '''
He is a good boy
She is a good girl
We are very nice
Hello boy hello boy
hello girl hello girl
hello dog
hello cat
hello pig
'''
speech = speech_text.lower().split()
dic = {}
for word in speech:
if word not in dic:
dic[word] = 1 #给词典赋值
else:
dic[word] = dic[word] + 1
swd = sorted(dic.items(), key = operator.itemgetter(1),reverse = True)
#stop_words
for k,v in swd:
if k not in stop_words:
print(k,v) print(swd)
方法二:
import operator
from nltk.corpus import stopwords
stop_words = stopwords.words('English')#目的是去除人称代词等,注意根据编译提示下载相应库 speech_text = '''
He is a good boy
She is a good girl
We are very nice
Hello boy hello boy
hello girl hello girl
hello dog
hello cat
hello pig
'''
speech = speech_text.lower().split()
from collections import Counter
c = Counter(speech)
for sw in stop_words:
del c[sw]
print(c.most_common(10)) #打印前10项
python统计字词练习的更多相关文章
- python统计元素重复次数
python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...
- 简易安装python统计包
PythonCharm简易安装python统计包及 本文介绍使用pythonCharm IDE 来安装Python统计包或一些packages的简单过程,基本无任何技术难度,顺便提一提笔者在安装过程中 ...
- Python统计列表中的重复项出现的次数的方法
本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...
- Python统计日志中每个IP出现次数
介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下 本脚本可用于多种日志类型 #-*- coding:utf-8 -* ...
- python 统计时间,写日志
python 统计时间使用time模块,写日志使用logging模块,这两个都是标准模板. 测试socket使用socket模块 # 统计时间 ---------------------- impor ...
- python统计文本中每个单词出现的次数
.python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...
- python统计文档中词频
python统计文档中词频的小程序 python版本2.7 效果如下: 程序如下,测试文件与完整程序在我的github中 #统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值 def ...
- python统计字符串里每个字符的次数
方法一: 推导式 dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas" print {i:dd.count(i) for i in dd} 方法二: co ...
- python 统计使用技巧
python 统计使用技巧 # 1.不输入回车获取值 注:需要tty模块配合. fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ...
随机推荐
- elk每日清除30天索引脚本
日常elk产生日志太多,故写个脚本放在定时任务,定时清理脚本 查询索引: curl -XGET 'http://127.0.0.1:9200/_cat/indices/?v' 删除索引: cu ...
- python4 分支结构,循环结构 for循环
## 复习 ```python'''1.变量名命名规范 -- 1.只能由数字.字母 及 _ 组成 -- 2.不能以数字开头 -- 3.不能与系统关键字重名 -- 4._开头有特殊含义 -- 5.__开 ...
- 集成Tomcat环境到Eclipse中
集成Tomcat环境到Eclipse中 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 一.安装Eclipse环境 1>.安装JDK环境 官方地址:https://www.or ...
- Java NIO系列教程(一) Java NIO 概述
<I/O模型之四:Java 浅析I/O模型> 一.阻塞IO与非阻塞IO 阻塞IO: 通常在进行同步I/O操作时,如果读取数据,代码会阻塞直至有 可供读取的数据.同样,写入调用将会阻塞直至数 ...
- MarkDown 的两种页内跳转方法!!!!!
页面内跳转就是点击某个文本,能够跳转到页面里指定的其他地方,经常用于目录中. 第一种是利用Html5 比如点击Feature, 跳转到features中 MarkDown: [Feature](#1) ...
- APPLE-SA-2019-3-25-1 iOS 12.2
APPLE-SA-2019-3-25-1 iOS 12.2 iOS 12.2 is now available and addresses the following: CFStringAvailab ...
- Java 线程安全LocalTime 和LocaldateTime 新的Date和Time类 -JDK8新时间类的简单使用
不可变类且线程安全 LocalDate .java.time.LocalTime 和LocaldateTime 新的Date和Time类 DateTimeFormatter ==https://ww ...
- 【先验知识归纳】Flask快速入门
本文参考:快速入门 - Flask 0.10.1 文档 路由 Flask使用route修饰器来关联URL与程序函数: @app.route('/') def hello_world(): return ...
- C语言之路-3-循环
1.while循环计算数字位数 #include<stdio.h> int main() { ; int x; printf("请输入数字:"); scanf(&quo ...
- anaconda3安装cv2模块(python3.6)