Python之words count
要求:
对文件单词进行统计,不区分大小写,并显示单词重复最多的十个单词
思路:
利用字典key,value的特性存单词及其重复的次数
每行进行特殊字符的处理,分离出被特殊字符包含的单词
def makekey(s:str)->list:
lst = []
s_complex = set(r"""!`#.,-*()\/[]*""") #利用集合装置特殊字符,前缀r不用转义
for word_i in s:
if word_i in s_complex:
lst.append(" ")
else:
lst.append(word_i)
new_string = "".join(lst).split()
return new_string src = '/tmp/sample.txt'
dic = {}
with open(src,'r') as f:
# f.readlines()
for line in f:
words_list=line.lower().split()
for word in words_list: #str in list
word = makekey(word) #return list
for words in word:
if words in dic.keys():
dic[words]+=1
else:
dic[words] = 1
reverse_dict = sorted(dic.items(),key=lambda x:x[1],reverse=True)
print(reverse_dict[:10])
Python之words count的更多相关文章
- Python中实现count(distinct )
假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...
- 【leetcode❤python】 204. Count Primes
#-*- coding: UTF-8 -*- #Hint1:#数字i,i的倍数一定不是质数,因此去掉i的倍数,例如5,5*1,5*2,5*3,5*4,5*5都不是质数,应该去掉#5*1,5*2,5*3 ...
- 【leetcode❤python】 38. Count and Say
#-*- coding: UTF-8 -*- class Solution(object): def countAndSay(self, n): """ ...
- [LeetCode&Python] Problem 696. Count Binary Substrings
Give a string s, count the number of non-empty (contiguous) substrings that have the same number of ...
- python中的count
count(self, sub, start=None, end = None)用于计算字符串中子序列的个数,sub, start=None, end = None定义查找范围,不写默认查找全部 举个 ...
- python学习之count()
定义: count()方法用于统计对象中,某个字符出现的次数 语法: str.count(sub, start= ,end=len(string)) sub:搜索的对象 start和end:搜索的范围 ...
- Python 字符串(count)
字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...
- 详解Python中的循环语句的用法
一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...
- Python Day1
一.安装python windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装到C盘下 3.配置环境变量 右键计算机属性---高级系统设置 ...
随机推荐
- SoftPixelEngin
目的,拓展知识. 1.CMake夸平台构建; 2.RederSystem; 3.Shaderlibrary: http://blog.csdn.net/ym19860303/article/detai ...
- 在dbgrideh中允许选择多行,如何知道哪些行被选中
是个BOOKMARK类型的属性. SelectedRows: TBookmarkList procedure TForm1.Button1Click(Sender: TObject); var i, ...
- LeetCode 937 Reorder Log Files 解题报告
题目要求 You have an array of logs. Each log is a space delimited string of words. For each log, the fi ...
- flex布局 响应式布局
移动端页面开发流程 移动端页面布局 一.移动端app分类 1.Native App原生app手机应用程序 使用原生的语言开发的手机应用,Android系统用的是java,ios系统用的是objec ...
- RSA 时序攻击
RSA的破解从理论上来讲是大数质数分解,可是就是有一些人另辟蹊径,根据你解密的时间长短就能破解你的RSA私钥. 举一个不恰当但是比较容易理解的例子: 密文0101 私钥0110 明文0100 问题的关 ...
- MongoDB 查询$关键字 $in $or $all
属于:$in 满足其中一个元素的数据把age=13,73 的数据显示 > db.user.find({age: { $in:[13,73]}}) { "_id" : Obje ...
- MySQL中的数据类型以及完整性约束
数据类型 数据库mysql中也是分很多数据类型的,最常用的就是:数字类型.字符类型.日期类型.枚举与集合类型 一.数字类型: 默认都是有符号的,即正负号,若想无符号,在创建表时加unsigned.指定 ...
- what's the python之函数及装饰器
what's the 函数? 函数的定义:(return是返回值,可以没有,不过没有的话就返回了None) def wrapper(参数1,参数2,*args,默认参数,**kwargs): '''注 ...
- 前端框架之Vue(8)-表单输入绑定
基础用法 你可以用 v-model 指令在表单 <input> . <textarea> 及 <select> 元素上创建双向数据绑定.它会根据控件类型自动选取正确 ...
- vue中filter的用法
test() { var arr1 = ["A", "B", "C","C","A"]; var r ...