要求:

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

思路:

利用字典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. OWA (Office Web Access)

    exchange的web网页,可以enrich的打开,用起来还行outlook一样. 同事的chrome(under windows) 默认就是i这样的.也没装插件,也没有怎样. 我的chrome(u ...

  2. JavaScript, DOM查找元素

    1.document.getElementById("id"); => IE8 及较低版本不区分ID的大小写 => IE7及较低版本中表单元素的name特性和ID都会被 ...

  3. 20180409 Code First

    many people use DB First,Today I see Code First.  这部分,百度上面有更多详细的资料,虽然不明白Migrations内部的机制,但是还是可以记录一下 打 ...

  4. 这套方法论,彻底终结MySQL同步延迟问题

    作者介绍 张秀云,网名飞鸿无痕,现任职于腾讯,负责腾讯金融数据库的运维和优化工作.2007年开始从事运维方面的工作,经历过网络管理员.Linux运维工程师.DBA.分布式存储运维等多个IT职位.对Li ...

  5. Windows 10正式版的历史版本

    1.Windows 10 1507 初版Windows 10,代号TH1,版本号10240,发布于2015年7月. 2015年7月29日,微软正式发布了Windows 10操作系统.Windows 1 ...

  6. UltraISO 9.7.0.3476中文完美破解安装版

    https://cn.ultraiso.net/uiso9_cn.exe 简体中文版专用:        注册名:Guanjiu 注册码:A06C-83A7-701D-6CFC 多国语言版专用: 注册 ...

  7. 百度接口test

    https://aip.baidubce.com/oauth/2.0/token?grant_type=client_credentials&client_id=Mk2Orf5pqEOXvYR ...

  8. mysql相关SQL

    1.mysql分组获取最新数据 sql> select max(column_name) from table group by column_name having count(*) orde ...

  9. 2019.03.23 Cookie

    Cookie  曲奇饼干  哈哈哈.通俗的将,应该是发票. 因为http是无状态操作 当你访问服务器之后,应该会给你响应发票Cookie记录你访问了什么东西 便于下次再来查找吧,Cookie有时间的限 ...

  10. ASP.NET 预编译命令(解决发布后第一次访问慢问题)

    ASP.NET 编译工具 (Aspnet_compiler.exe) 官方说明 新建bat文件   @echo off   CD /d C:\Windows\Microsoft.NET\Framewo ...