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 ...
随机推荐
- 【零基础学习FreeRTOS嵌入式系统】之一:FreeRTOS环境搭建
[零基础学习FreeRTOS嵌入式系统]之一:FreeRTOS环境搭建 一:FreeRTOS系统下载 在官网上https://www.freertos.org/,找到下载入口. 或直接进入下载地址ht ...
- css3 常用动画 随笔
/* animation */.a-bounce,.a-flip,.a-flash,.a-shake,.a-swing,.a-wobble,.a-ring{-webkit-animation:1s e ...
- Drupal7安装注意事项
1.在php.ini中将max_execution_time = 2400,memory_limit = 256M
- Css选择器(上) 让样式无孔不入
css选择器 一个可以选择样式的工具, 这里适用于无论是内部代码还是外部引用 abc.css 这类型的文件. 基本选择器*{ } 就是一个简单的*, 代表应用于全部. 不适合于个性 ...
- C/C++遍历进程和进程ID的小工具
原文:http://blog.csdn.net/qq78442761/article/details/54646010 当我们写某些具有破坏性的程序时就需要对进程进行遍历和提取ID 对于上述功能,我们 ...
- Ubuntu 16.04 Apache2 更改访问html根路径方案(可解决403)
(1)确定html文件在服务器主机上的部署路径.例:/home/rl/vc/ (2)修改 vim sites-enabled/000-default.conf 中 DirectoryRoot 为 : ...
- Ubuntu更换硬盘
0x00 背景: 目前ubuntu用的是一个80g的硬盘( 一开始没买硬盘,直接拆了一个老老老老的机子来用),系统16.04, 只有一个/分区 ( /dev/sdb1 ) . 新的硬盘是一块256G的 ...
- java将int类型的变量转化成String类型的
第一种方法:String的valueOf方法,int i=5;String s=String.valueOf(i);第二种方法,直接在int后面加一个空的字符串,因为在java里面,默认任务int类型 ...
- java笔记--正则表达式的运用(包括电话,邮箱验证等)
正则表达式 --如果朋友您想转载本文章请注明转载地址"http://www.cnblogs.com/XHJT/p/3877402.html "谢谢-- 正则表达式符号:" ...
- ASP.NET错误处理的方式(一)
对Web应用程序来说,发生不可预知的错误和异常在所难免,我们必须为Web程序提供错误处理机制.当错误发生时,我们必须做好两件事情:一是将错误信息记录日志,发邮件通知网站维护人员,方便技术人员对错误进行 ...