方法一:

 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. Eclipse 运行导入的 Java 项目时,Error:A JNI error has occurred

    出现场景 导入 Java 项目,运行时,出现:Error:A JNI error has occurred.... 解决方式 该项目的 Build Path , 在Libraries 中删除后重新添加 ...

  2. Web of science数据下载以数据处理

    目标网站分析 我们要获取的就是这几个数值 程序实现 # -*- coding: utf-8 -*- """ @Datetime: 2019/2/28 @Author: Z ...

  3. OS + macOS Mojave 10.14.4 / sushi / ssh-keygen / ssh-copy-id

    s 系统版本: macOS 10.14.4 (18E226) 内核版本: Darwin 18.5.0 型号名称: Mac mini 2014 型号标识符: Macmini7,1 处理器名称: Inte ...

  4. kubernetes云平台管理实战: pod资源共享(三)

    一.共享容器IP地址 1.查看容器进程 [root@k8s-node1 ~]# docker ps -a CONTAINER ID IMAGE COMMAND CREATED STATUS PORTS ...

  5. JAVA进阶9

    间歇性混吃等死,持续性踌躇满志系列-------------第9天 1.使用throw语句抛出异常 在通常情况下,程序发生错误时系统会自动抛出异常,而有时希望程序自动抛出异常,可以使用throw语句来 ...

  6. 🍓rem单位在Chrome中字体大小异常 🍓

    这是一个很少出现,而一旦出现就让人头疼难当的bug.....网上资料比较少. 解决:https://oss.so/article/87 即为html标签设置-webkit-text-size-adju ...

  7. 【原创】大叔问题定位分享(18)beeline连接spark thrift有时会卡住

    spark 2.1.1 beeline连接spark thrift之后,执行use database有时会卡住,而use database 在server端对应的是 setCurrentDatabas ...

  8. lua分割字符串

    str = "abc;123;345" local tab = string.split(str, ";") 然后list里面就是 abc123345 了.第二 ...

  9. Python 爬虫 NewCnblogs (爬虫-Django-数据分析)

    需求分析 数据库架构 注册 登录 首页 个人站点 文章+评论 后台 爬虫 数据分析 添加搜索+已上线

  10. unity setactive的使用

    1.可以用本身移出布局来实现隐藏 2.RawImage的texture的设置生成的一定要及时消除,避免内存泄漏