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 Boot入门——thymeleaf模板使用
使用步骤 1.在pom.xml中引入thymeleaf <!-- thymeleaf插件 --> <dependency> <groupId>org.springf ...
- Unity3D-UGUI图集打包与动态使用(TexturePacker)
参考地址: http://blog.csdn.net/cjsen/article/details/52487706 今天做项目大佬看我在做图集,就跟我说可以用工具打包图集,也就是TexturePack ...
- 选择排序的php实现 Selection Sort
选择排序Selection Sort的PHP实现,安全按照算法所写. 同一排序算法下,需要趟数最多的数列是什么数列呢?思考中. 每一趟从待排序的数据元素中选出最小(或最大)的一个元素,顺序放在已排好序 ...
- Postman工具——请求与响应
两个内容: Request 请求和 Response 响应,下面就开始了. 一.Request 请求 Request 请求,我们只介绍常用的四种:GET.POST.PUT.DELETE,其他类型的就不 ...
- html5 frameset5内嵌框架集
利用html5 frameset内嵌框架简单做一个网页,网页的布局大体如下: 我们可以将其分为四个部分: 第一部分:top图片栏 第二部分:left链接栏 第三部分:right内容栏 第四部分:网页整 ...
- web版源码管理软件SCM-Manager使用简要说明
登录 默认管理员用户:scmadmin / scmadmin 用户 用户,可以反向添加针对所有仓库的权限 用户组 用户组,可以反向添加针对所有仓库的权限 用户组管理 用户组,可委托给具体用户进行管理( ...
- SVN的安装和使用
1.安装 下载SVN,一直默认安装 安装成功后,配置环境变量path=C:\Program Files\TortoiseSVN\bin 验证SVN安装是否成功:adb -help 或 adb -ver ...
- Java 代码复用 —— 泛型
public interface Comparable<T> { public int compareTo(T o); } 1. 接口(Comparable:可比较接口) public s ...
- LeetCode Sum of Square Numbers
原题链接在这里:https://leetcode.com/problems/sum-of-square-numbers/description/ 题目: Given a non-negative in ...
- JDBC小常识
1 JDBC连接数据库6步 Load the JDBC Driver Establish the Database Connection Create a Statement Object Execu ...