方法一:

 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统计字词练习的更多相关文章

  1. python统计元素重复次数

    python统计元素重复次数 # !/usr/bin/python3.4 # -*- coding: utf-8 -*- from collections import Counter arr = [ ...

  2. 简易安装python统计包

    PythonCharm简易安装python统计包及 本文介绍使用pythonCharm IDE 来安装Python统计包或一些packages的简单过程,基本无任何技术难度,顺便提一提笔者在安装过程中 ...

  3. Python统计列表中的重复项出现的次数的方法

    本文实例展示了Python统计列表中的重复项出现的次数的方法,是一个很实用的功能,适合Python初学者学习借鉴.具体方法如下:对一个列表,比如[1,2,2,2,2,3,3,3,4,4,4,4],现在 ...

  4. Python统计日志中每个IP出现次数

    介绍了Python统计日志中每个IP出现次数的方法,实例分析了Python基于正则表达式解析日志文件的相关技巧,需要的朋友可以参考下 本脚本可用于多种日志类型 #-*- coding:utf-8 -* ...

  5. python 统计时间,写日志

    python 统计时间使用time模块,写日志使用logging模块,这两个都是标准模板. 测试socket使用socket模块 # 统计时间 ---------------------- impor ...

  6. python统计文本中每个单词出现的次数

    .python统计文本中每个单词出现的次数: #coding=utf-8 __author__ = 'zcg' import collections import os with open('abc. ...

  7. python统计文档中词频

    python统计文档中词频的小程序 python版本2.7 效果如下: 程序如下,测试文件与完整程序在我的github中 #统计空格数与单词数 本函数只返回了空格数 需要的可以自己返回多个值 def ...

  8. python统计字符串里每个字符的次数

    方法一: 推导式 dd="ewq4aewtaSDDSFDTFDSWQrtewtyufashas" print {i:dd.count(i) for i in dd} 方法二: co ...

  9. python 统计使用技巧

    python 统计使用技巧 # 1.不输入回车获取值 注:需要tty模块配合. fd = sys.stdin.fileno() old_settings = termios.tcgetattr(fd) ...

随机推荐

  1. 2017-12-19python全栈9期第四天第二节之列表的增删查改之按索引改和按切片改

    #!/user/bin/python# -*- coding:utf-8 -*-li = ['zs','ls','ww','zl','xx']li[0] = 'cxvb' #按索引位置print(li ...

  2. Numpy 系列(九)- 结构化数组

      简介 之前我们操作Numpy的数组时,都是通过索引来操作的.针对二维数组,使用索引可以完成对行.列的操作.但是这是非常不直观的.可以把二维数组想象成一个excel表格,如果表格没有列名,操作起来会 ...

  3. Linux性能优化实战:到底应该怎样理解平均负载(02)

    一.平均负载与CPU使用率并没有直接关系 1.平均负载 单位时间内,系统处于可运行状态和不可终端状态的平均进程数也就是平均活跃进程数,它和cpu使用率并没有直接关系, 可运行状态: 正在使用的cpu或 ...

  4. docker 安装入门

    install docker 命令 docker version // docker 版本 docker pull nginx // 拉取nginx docker images // 查看本机dock ...

  5. 新年 flag

    在浮躁的年代本不该如此贪多,奈何鸭梨山大...温故知新吧 GO中文社区 深入学习一两门新的编程语言: -Go编程基础 -Go Web基础 -Go名库讲解 rustlang 中文文档 知乎板块 GO 知 ...

  6. 怎样以快速样式的方式在word文档中生成以下多级自动编号

    本篇博文简单介绍一下在word中利用快速样式生成多级编号的方法. 一.自定义多级列表格式: 1.点击,开始--段落--多级列表--定义新的多级列表: 2.设置一级编号: (1)在"此级的编号 ...

  7. How to learn PDE (怎么学偏微分方程)

    To learn PDE, you need some knowledge of physics (to build up the intuition), solid training of anal ...

  8. v-charts 和 websocket实现数据展示动态推送

    v-charts https://v-charts.js.org/#/ ELEMENT力作: 在使用 echarts 生成图表时,经常需要做繁琐的数据类型转化.修改复杂的配置项,v-charts 的出 ...

  9. COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问。最新解决方案

    检索 COM 类工厂中 CLSID 为 {000209FF-0000-0000-C000-000000000046} 的组件失败,原因是出现以下错误: 80070005 拒绝访问. (异常来自 HRE ...

  10. Spring系列(零) Spring Framework 文档中文翻译

    Spring 框架文档(核心篇1和2) Version 5.1.3.RELEASE 最新的, 更新的笔记, 支持的版本和其他主题,独立的发布版本等, 是在Github Wiki 项目维护的. 总览 历 ...