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.配置环境变量 右键计算机属性---高级系统设置 ...
随机推荐
- Python多线程中阻塞(join)与锁(Lock)的使用误区
参考资料:https://blog.csdn.net/cd_xuyue/article/details/52052893 1使用两个循环分别处理start和join函数.即可实现并发. threads ...
- EntityFramework_基础
什么是EF EF是一种ORM(Object-relational mapping)框架,它能把我们在编程时使用对象映射到底层的数据库结构.它可以让应用程序开发者将关系型数据作为业务模型来使用,也消除了 ...
- crm项目整理
crm项目整理 一.开发背景 由于公司人员的增多,原来通过excel表格存取方式过于繁琐,而且对于公司人员的调配和绩效考核等不能做到精确处理,所以开发crm系统,开始开发只是针对销售人员和客户,后 ...
- LeetCode 349 Intersection of Two Arrays 解题报告
题目要求 Given two arrays, write a function to compute their intersection. 题目分析及思路 给定两个数组,要求得到它们之中共同拥有的元 ...
- Appium环境配置(二)
一.使用Eclipse直接创建案例工程 1.打开Eclipse,[File]-->[New]-->[Project] 2.选择[Java Project]-->[Next] 3.输入 ...
- 【python基础】利用pandas处理Excel数据
参考:https://www.cnblogs.com/liulinghua90/p/9935642.html 一.安装第三方库xlrd和pandas 1:pandas依赖处理Excel的xlrd模块, ...
- VS2013 opencv配置
有三个地方需要配置,在配置之前首先要将platform配置好,下面的例子是x64 Release的“ 然后需要将include.lib的路径配置好 然后将dll拷贝至编译生成的Release文件夹中即 ...
- replace函数的使用(替换单个和全局)
//单个 var aa="1-2-3"; aa=aa.replace(',',"."); console.log(aa); //全局替换 var aa=&quo ...
- phpmyadmin无法访问503错误的解决方法
昨天ytkah更新了一些服务器软件,今天访问数据库居然出现503错误,主要提示如下.点开phpmyadmin设置,查看了一下端口,没有改动:重启了一下phpmyadmin也不能运行:再看了一下php版 ...
- finecms同时调用子栏目和子栏目的文章怎么操作
之前ytkah写过dedecms如何调用当前栏目的子栏目及子栏目文章,那如果是finecms如何同时调用子栏目和子栏目的文章呢? {list action=category pid=0 id=31} ...