要求:

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

思路:

利用字典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. xcode工程编译错误:error: Couldn’t materialize

    错误信息: error: Couldn't materialize: couldn't get the value of variable amount: variable not available ...

  2. MIPS 指令集开源了

    去年年底我们报导过 MIPS 指令集将于今年第一季度开源的消息,现在 MIPS 官方已经正式将其释出. MIPS 是一种精简指令集(Reduced Instruction Set Computer,R ...

  3. IntelliJ常用设置及快捷键

    转自: http://www.blogjava.net/rockblue1988/archive/2014/10/25/418994.html 一.黑色主题 Darcula眼睛舒服,最重要的是酷!设置 ...

  4. Win7 开机自动启动Outlook2010

    工作中,设置一些开机启动项,可以提供工作效率,下面演示设置Outlook2010在win7系统开机自启动,其它软件,如Eclipse.微信等,也可以这么配置. 环境: win7,outlook 201 ...

  5. python server

    #!/usr/bin/env python #coding=utf-8 # modifyDate: 20120808 ~ 20120810 # 原作者为:bones7456, http://li2z. ...

  6. 【Python】脚本运行报错:IndentationError: unindent does not match any outer indentation level

    [问题] 一个python脚本,本来都运行好好的,然后写了几行代码,而且也都确保每行都对齐了,但是运行的时候,却出现语法错误: IndentationError: unindent does not ...

  7. Testlink1.9.5的安装配置

    前两天搭建了Testlink环境,在这里整理记录下过程中遇到的问题以及搭建流程.Testlink版本:1.9.5操作系统:Windows7 32bit 步骤一:安装XAMPP 下载解压xampp压缩包 ...

  8. 帝国cms内容关键字自动加链接且设置内容关键字只替换一次

    网站上线前先设置一些内部链接对后期的优化排名很有帮助,帝国cms也可以设置文章中的关键字自动加链接,但是要注意一下关键词替换次数,最好是1次. 怎么操作呢?分两步完成 1.帝国cms文章关键字自动加链 ...

  9. Oracle(2)之多表查询&子查询&集合运算

    多表查询 笛卡尔积 同时查询多张表时,每张表的每条数据都要和其它表的每条数据做组合.如下栗子,我们发现产生的总记录数是 56 条,还发现 emp 表是 14 条,dept 表是 4 条,56 条正是 ...

  10. RN-ios模拟器上调出中文输入法

    react-native 项目:在ios模拟器上需要拼写汉字,步骤是, 1.在模拟器的设置-通用-语言与地区-iphone语言设置为:简体中文 2.模拟器的 Hardware-Keyboard-勾选下 ...