python取一个字符串中最多出现次数的词
#-*- coding:utf-8 -*-
#取一个字符串中最多出现次数的词
import re
from collections import Counter my_str = """
Simple is better than complex.
Complex is better than complicated.
Flat is better than nested.
Sparse is better than dense.
Readability counts.
Special cases aren't special enough to break the rules.
Although practicality beats purity.
Errors should never pass silently.
Unless explicitly silenced.
In the face of ambiguity, refuse the temptation to guess.
There should be one-- and preferably only one --obvious way to do it.
Although that way may not be obvious at first unless you're Dutch.
Now is better than never.
Although never is often better than *right* now.
If the implementation is hard to explain, it's a bad idea.
If the implementation is easy to explain, it may be a good idea.
Namespaces are one honking great idea -- let's do more of those!
""" word_count = Counter(re.split('[ .,;~!-<>/|@#$%^&*?\n\t]', my_str))
#取非空的前八个最多单词
most_11 = [x for x in word_count.most_common(8) if x[0] != ""]
#直接取前八个最多单词
print word_count.most_common(8)
print(most_11)
for i in most_11:
print i[0]
python取一个字符串中最多出现次数的词的更多相关文章
- Java实现统计某字符串在另一个字符串中出现的次数
面试时会经常考这样的题目,估计也不让使用正则表达式.还好这个算法还算简单,不过在草稿纸上写难免会出现运行异常,好吧,面试官赢了,乃们屌丝就实实在在的把代码码出来吧. 谢谢“心扉”对我代码bug的纠正, ...
- C语言:利用指针解决:统计一个长度为2的字符串在另外一个字符串中出现的次数。
//统计一个长度为2的字符串在另外一个字符串中出现的次数. #include <conio.h> #include <stdio.h> #include <string. ...
- Python判断一个字符串中是否存在多个子串中的一个
在使用python的开发过程中,常常需要判断,字符串中是否存在子串的问题, 但判断一个字符串中是否存在多个字串中的一个时,如if (a or b) in c或者if x contains a|b|c| ...
- MSSQL sqlserver 统计"一个字符串"在"另一个字符串"中出现的次数的方法
转自 http://www.maomao365.com/?p=9858 摘要: 下文讲述sqlserver中最快获取一个字符串在另一个字符串中出现个数的方法分享 实验环境:sql server 20 ...
- python之统计字符串中字母出现次数
dic=dict() d={} s=set() s='helloworld' (1)d=dict() for x in s: if x not in d.keys(): d[x]=1 else: d[ ...
- php 计算一个字符串在另一个字符串中出现的次数
<?php $text = 'This is a test'; echo strlen($text); // 14 echo substr_count($text, 'is'); // 2 // ...
- JS求一个字符串在另一个字符串中出现的次数
参数说明: subString子字符串 originString母字符串 isIgnoreCap是否忽略大小写,默认忽略 function stringFre(subString, originStr ...
- SQL语句 查询同一个字符在某一个字符串中出现的次数
select len(replace(字段名A,';','--'))-len(字段名A) from table表名
- 一个字符串中可能包含a~z中的多个字符,如有重复,如String data="aavzcadfdsfsdhshgWasdfasdf",求出现次数最多的那个字母及次数,如有多个重复的则都求出。
主要掌握String中的方法 char[] toCharArray() 将此字符串转换为一个新的字符数组. int indexOf(String str) 返回 ...
随机推荐
- Spring Cloud遇到的坑——服务状态为DOWN
今天启动上次写好的微服务时出现了这样的情况: 于是一脸懵逼... 忽然打开DOWN状态微服务的pom文件,看到如下情况: <dependencies> <dependency> ...
- review05
子类与继承 所有类都是Object的子孙类. 子类继承了父类的成员变量和方法,就好像是在子类中直接定义了一样. 如果子类和父类在同一个包中,子类自然地继承了父类中不是private的成员变量和方法. ...
- json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException
转:json数据转换异常:net.sf.json.JSONException: java.lang.reflect.InvocationTargetException 执行:JSONArray arr ...
- Model compatibility cannot be checked because the database does not contain model metadata. Ensure that IncludeMetadataConvention has been added to the DbModelBuilder conventions
Model compatibility cannot be checked because the database does not contain model metadata. Ensure t ...
- java常用封装方法
public Map<String,String> getDateByStr(String str,String startDate,String endTime){ Map<Str ...
- hdu 5238 Calculator(线段树,中国剩余定理¥)
Calculator Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- 51nod 1105 二分好题
http://www.51nod.com/onlineJudge/questionCode.html#!problemId=1105 1105 第K大的数 基准时间限制:1 秒 空间限制:131072 ...
- MySQL 索引 视图 触发器 存储过程 函数
1.索引 索引相当于图书的目录,可以帮助用户快速的找到需要的内容. 数据库利用各种各样的快速定位技术,能够大大提高查询效率.特别是当数据量非常大,查询涉及多个表时,使用索引往往能使查询速度加快成千上万 ...
- nyoj-158-省赛来了(组合数)
题目链接 /* Name:nyoj-158-省赛来了 Copyright: Author: Date: 2018/4/25 17:07:22 Description: 暴力,秒天秒地 */ #incl ...
- New Concept English three (52)
My cousin, Harry, keeps a large curiously-shaped bottle on permanent display in his study. Despite t ...