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的更多相关文章

  1. python sorted排序

    python sorted排序 Python不仅提供了list.sort()方法来实现列表的排序,而且提供了内建sorted()函数来实现对复杂列表的排序以及按照字典的key和value进行排序. s ...

  2. python 使用set对列表去重,并保持列表原来顺序

    # python 使用set对列表去重,并保持列表原来顺序 list1 = ['cc', 'bbbb', 'afa', 'sss', 'bbbb', 'cc', 'shafa'] for item i ...

  3. python sorted排序用法详解

    sorted排序 python sorted 排序 1. operator函数在介绍sorted函数之前需要了解一下operator函数. operator函数是python的内置函数,提供了一系列常 ...

  4. Python 元组 count() 方法

    描述 Python 元组 count() 方法用于统计某个元素在元祖中出现的次数. 语法 count() 方法语法: T.count(obj) 参数 obj -- 元祖中统计的对象. 返回值 返回元素 ...

  5. Python 列表 count() 方法

    描述 Python 列表 count() 方法用于统计某个元素在列表中出现的次数. 语法 count() 方法语法: L.count(obj) 参数 obj -- 列表中统计的对象. 返回值 返回元素 ...

  6. python计数器Count

    python计数器Count # -*- coding:utf-8 -*- """ python计数器Counter 需导入模块collections "&qu ...

  7. python中出现 IndentationError:unindent does not match any outer indentation level

    python中出现IndentationError:unindent does not match any outer indentation level 今天在网上copy的一段代码,代码很简单,每 ...

  8. Python sorted 函数

    Python sorted 函数 sorted 可以对所有可迭代的对象进行排序操作,sorted 方法返回的是一个新的 list,而不是在原来的基础上进行的操作.从新排序列表. sorted 语法: ...

  9. Python sorted list的实现

    Python sorted list的实现 具体思路是用二分保list有序+插入 class SortedList(list): K = -1 def __init__(self, K=-1): li ...

随机推荐

  1. 插件式WebApi服务及自动生成Api帮助文档

    上一篇博客中,讲到了将WebApi Host到控制台和IIS,本篇总结一下如何将WebApi的Service以插件的形式进行动态部署,并设置Hoster的首页显示Api帮助文档,当然,也包括动态部署进 ...

  2. grunt-contrib-watch 监控 JS 文件改变来运行预定义的Tasks

    依赖于 GruntJs ~0.4.0 监控 JS 文件改变来运行预定义的Tasks Demo: watch: { scripts: { files: ['src/**/*.js'], tasks: [ ...

  3. bitset(01串)优化

    bitset的经典使用: 见代码及注释: #include<bitset> #include<algorithm> using namespace std; //只需调用< ...

  4. Node.js 优雅地自动审核团队的代码

    Node.js® is a JavaScript runtime built on Chrome's V8 JavaScript engine. 简介 在团队开发中,无论是写前端(js,css,htm ...

  5. [算法练习]Reverse Integer

    题目说明: Reverse digits of an integer. Example1: x = 123, return 321 Example2: x = -123, return –321   ...

  6. android studio新建项目时出现Error:Execution failed for task ':app:preDebugAndroidTestBuild'.

    android studio更新后创建新项目时出现以下错误 可以用Build->Rebuild Project解决,但这个方法只是临时的,重新打开项目还是会报错 所以用另一种方法: 在app下的 ...

  7. [转]Apache的CRT格式SSL证书转换成IIS用的PFX格式

    转自:http://www.getvm.net/apache-crt-ssl-convert-to-iis-pfx/ Apache使用的SSL证书是.crt格式,如果你的网站从Apache换到了win ...

  8. matplotlib.pyplot 导引

    matplotlib.pyplot 是采用 python 语言和使用数值数学库 numpy 数组数据的绘图库.其主要目标是用于数据的可视化显示. 输出图形组成 matplotlib.pyplot 模块 ...

  9. androidandroid中的通过网页链接打开本地app

    http://blog.csdn.net/zjlovety/article/details/54847980 <html> <head> <meta http-equiv ...

  10. 使用公钥和私钥实现LINUX下免密登录

    linux公钥私钥实现无密码登录 首先本地主机生成公约和私钥 # ssh-keygen               /生成公钥和私钥 不要更改默认路径,中途不要输入密码,直接两次回车. 2.  将生成 ...