python sorted() count() set(list)-去重 -- search + match
2、用python实现统计一篇英文文章内每个单词的出现频率,并返回出现频率最高的前10个单词及其出现次数,并解答以下问题?(标点符号可忽略)
(1) 创建文件对象f后,解释f的readlines和xreadlines方法的区别?
(2) 追加需求:引号内元素需要算作一个单词,如何实现?
cat /root/text.txt
hello world 2018 xiaowei,good luck
hello kitty 2017 wangleai,ha he
hello kitty ,hasd he
hello kitty ,hasaad hedsfds
#我的脚本
#!/usr/bin/python
#get ['a','b','c']
import re
with open('/root/text.txt') as f:
openfile = f.read()
def get_list_dict():
word_list = re.split('[0-9\W]+',openfile)
list_no_repeat = set(word_list)
dict_word = {}
for each_word in list_no_repeat:
dict_word[each_word] = word_list.count(each_word)
del dict_word['']
return dict_word
#{'a':2,'c':5,'b':1} => {'c':5,'a':2,'b':1}
def sort_dict_get_ten(dict_word):
list_after_sorted = sorted(dict_word.items(),key=lambda x:x[1],reverse=True)
print list_after_sorted
for i in range(3):
print list_after_sorted[i][0],list_after_sorted[i][1]
def main():
dict_word = get_list_dict()
sort_dict_get_ten(dict_word)
if __name__ == '__main__':
main()
[('hello', 4), ('kitty', 3), ('he', 2), ('good', 1), ('hasd', 1), ('wangleai', 1), ('hasaad', 1), ('xiaowei', 1), ('hedsfds', 1), ('luck', 1), ('world', 1), ('ha', 1)]
hello 4
kitty 3
he 2
python sorted() count() set(list)-去重 -- search + match的更多相关文章
- python sorted排序
python sorted排序 Python不仅提供了list.sort()方法来实现列表的排序,而且提供了内建sorted()函数来实现对复杂列表的排序以及按照字典的key和value进行排序. s ...
- python 使用set对列表去重,并保持列表原来顺序
# python 使用set对列表去重,并保持列表原来顺序 list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] for item i ...
- python sorted排序用法详解
sorted排序 python sorted 排序 1. operator函数在介绍sorted函数之前需要了解一下operator函数. operator函数是python的内置函数,提供了一系列常 ...
- Python 元组 count() 方法
描述 Python 元组 count() 方法用于统计某个元素在元祖中出现的次数. 语法 count() 方法语法: T.count(obj) 参数 obj -- 元祖中统计的对象. 返回值 返回元素 ...
- Python 列表 count() 方法
描述 Python 列表 count() 方法用于统计某个元素在列表中出现的次数. 语法 count() 方法语法: L.count(obj) 参数 obj -- 列表中统计的对象. 返回值 返回元素 ...
- python计数器Count
python计数器Count # -*- coding:utf-8 -*- """ python计数器Counter 需导入模块collections "&qu ...
- python中出现 IndentationError:unindent does not match any outer indentation level
python中出现IndentationError:unindent does not match any outer indentation level 今天在网上copy的一段代码,代码很简单,每 ...
- Python sorted 函数
Python sorted 函数 sorted 可以对所有可迭代的对象进行排序操作,sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作.从新排序列表. sorted 语法: ...
- Python sorted list的实现
Python sorted list的实现 具体思路是用二分保list有序+插入 class SortedList(list): K = -1 def __init__(self, K=-1): li ...
随机推荐
- ES6学习笔记(五)-函数扩展
- SQL-字符串运算符和函数
COALESCE(columnname,string) 函数 将 NULL 值作为字符串(用空字符串或其他字符串替换 NULL)- 接受一列输入值(字段)如果该字段为 NULL,则返回后面替换的字符串 ...
- AngularJS+RequireJs实现动态加载JS和页面的方案研究【下】
about.js: [html] view plain copy 在CODE上查看代码片派生到我的代码片 define(['app'], function(app) { app.controller( ...
- Spring Data MongoDB 模糊查询
Pattern pattern = Pattern.compile("^.*" + value + ".*$"); Query query = new Quer ...
- SQL Server中【case...end】的用法
在SQL Server中 case...end 语句,一般有如下两种用法: 1.相当于C#中if...else,例: select CName,头衔=case when CLevel='A1' the ...
- SQL中的Datetime
在SQLserver中Datetime,只要格式是(yyyy-MM-dd HH:mm:ss)它都能认为是时间类型.
- nginx部署及简单优化
研究nginx优化时反复安装清理nginx,为方便做了一个简单部署脚本,用的最新稳定版1.14.0,默认路径,加入systemd系统进程管理中,可以通过systemd管理nginx的启动.终止.重载. ...
- .NET的那些事儿(9)——C# 2.0 中用iTextSharp制作PDF(基础篇) .
该文主要介绍如何借助iTextSharp在C# 2.0中制作PDF文件,本文的架构大致按照iTextSharp的操作文档进行翻译,如果需要查看原文,请点击一下链接:http://itextsharp. ...
- LED相关
P10 模组 分辨率32*16 尺寸320*160 间距 10mm P8 模组 分辨率32*16 尺寸256*128 间距 8mm P7.62 模组 分辨率 ...
- 定义类/实例(Class)
# -*- coding: UTF-8 -*- class pp(): '''Description''' def __init__(self,name): #初始化函数 self.nam ...