要求:

对文件单词进行统计,不区分大小写,并显示单词重复最多的十个单词

思路:

利用字典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的更多相关文章

  1. Python中实现count(distinct )

    假设一个表有6个字段c1,c2,c3,c4,c5,c6,有如下的sql语句: select c1,count(distinct(c6)) from tbl where c3>1 group by ...

  2. 【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 ...

  3. 【leetcode❤python】 38. Count and Say

    #-*- coding: UTF-8 -*- class Solution(object):    def countAndSay(self, n):        """ ...

  4. [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 ...

  5. python中的count

    count(self, sub, start=None, end = None)用于计算字符串中子序列的个数,sub, start=None, end = None定义查找范围,不写默认查找全部 举个 ...

  6. python学习之count()

    定义: count()方法用于统计对象中,某个字符出现的次数 语法: str.count(sub, start= ,end=len(string)) sub:搜索的对象 start和end:搜索的范围 ...

  7. Python 字符串(count)

    字符串 count:(python中的count()函数,从字面上可以知道,他具有统计功能) Python count() 方法用于统计字符串里某个字符出现的次数.可选参数为在字符串搜索的开始与结束位 ...

  8. 详解Python中的循环语句的用法

    一.简介 Python的条件和循环语句,决定了程序的控制流程,体现结构的多样性.须重要理解,if.while.for以及与它们相搭配的 else. elif.break.continue和pass语句 ...

  9. Python Day1

    一.安装python windows 1.下载安装包 https://www.python.org/downloads/ 2.安装 默认安装到C盘下 3.配置环境变量 右键计算机属性---高级系统设置 ...

随机推荐

  1. Page7:能控性、能观性及其判据和对偶原理(2)[Linear System Theory]

    内容包含连续时间时变系统的能控性和能观测性判据,离散时间线性系统的能控性和能观测性判据,以及对偶原理

  2. dhttp与IdCookieManager处理登陆过程

    dhttp与IdCookieManager处理登陆过程 我们知道,用IE注册网页(象论坛)时,它能够自动找出相应的Cookie并提交给服务器,从而使用户不用重新登录就能够看到与他自己帐号有关的内容.这 ...

  3. 图->连通性->有向图的强连通分量

    文字描述 有向图强连通分量的定义:在有向图G中,如果两个顶点vi,vj间(vi>vj)有一条从vi到vj的有向路径,同时还有一条从vj到vi的有向路径,则称两个顶点强连通(strongly co ...

  4. scala-数组/列表

    import scala.collection.mutable.ArrayBuffer var ary=Array(1,2,3) println(ary.mkString) println(ary(1 ...

  5. 洛谷P3041 视频游戏的连击Video Game Combos [USACO12JAN] AC自动机+dp

    正解:AC自动机+dp 解题报告: 传送门! 算是个比较套路的AC自动机+dp趴,,, 显然就普普通通地设状态,普普通通地转移,大概就f[i][j]:长度为i匹配到j 唯一注意的是,要加上所有子串的贡 ...

  6. 关于byte[]与string、Image转换

    byte[]与string转换 参考网址:https://www.cnblogs.com/xskblog/p/6179689.html 1.使用System.Text.Encoding.Default ...

  7. 关于WARN Dispatcher:68 - Could not find action or result报错

    出现这个错 00:03:37,142 WARN Dispatcher:68 - Could not find action or result: /crm/linkMan_addLinkMan.act ...

  8. 第三节:Windows下检出项目和提交项目

    1.将项目下载到本地: 2.在客户端中,右键点击test,选择show in explorer: 然后修改日志文件并保存: 3.在客户端中填写备注并提交: 4.提交到github中:

  9. AdvStringGrid常用操作

    AdvStringGrid1.Options:=[goEditing]; then CanEdit := True;end; //设置单元格对齐方式 procedure TForm1.AdvStrin ...

  10. cxLookupComboBox使用方法

    示例 //选择修改时执行procedure TForm1.cxLookupComboBox1PropertiesChange(Sender: TObject); begin edit1.Text:=V ...