name = input("Enter file:")
 if len(name) < 1 : name = "input.txt"
 fhand = open(name)

 counts = dict()
 for line in fhand:
     words = line.split()
     for word in words:
         # find the value that key is word, if not, return 0
         counts[word] = counts.get(word, 0) + 1

 lst = list()
 for key, val in counts.items():
     lst.append( (val, key) )

 lst.sort(reverse = True)
 for val, key in lst[:10]:
     print (key, val)

使用tuple统计文件中单词的个数的更多相关文章

  1. 统计文件中单词的个数---Shell及python版

    最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...

  2. 学c语言做练习之​统计文件中字符的个数

    统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ...

  3. JAVA实验--统计文章中单词的个数并排序

    分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...

  4. C语言算法--统计字符串中单词的个数

    #include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int le ...

  5. Python 统计文本中单词的个数

    1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...

  6. Linux统计文件中单词出现的次数

    grep -E "\b[[:alpha:]]+\b"  /etc/fstab  -o | sort | uniq -c 或 awk '{for(i=1;i<NF;i++){c ...

  7. Scala快速统计文件中特定单词,字符的个数

    val fileContent=Source.fromFile("/home/soyo/桌面/ss5.txt").getLines.mkString(",") ...

  8. sort +awk+uniq 统计文件中出现次数最多的前10个单词

    实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...

  9. java统计文本中单词出现的个数

    package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...

随机推荐

  1. 造成segmentation fault的可能原因分析

    一 造成segment fault,产生core dump的可能原因 1.内存访问越界 a) 由于使用错误的下标,导致数组访问越界 b) 搜索字符串时,依靠字符串结束符来判断字符串是否结束,但是字符串 ...

  2. 列存储压缩技巧,除公共除数或者同时减去最小数,字符串压缩的话,直接去重后用数字ID压缩

    Column-store compression At a high level, doc values are essentially a serialized column-store. As w ...

  3. Mysql存储过程及调用

     存储过程: 存储过程是SQL 语句和可选控制流语句的预编译集合,以一个名称存储并作为一个单元处理.存储过程存储在数据库内,可由应用程序通过一个调用执行,而且允许用户声明变量.有条件执行以及其它强大的 ...

  4. codeforces 707C C. Pythagorean Triples(数学)

    题目链接: C. Pythagorean Triples time limit per test 1 second memory limit per test 256 megabytes input ...

  5. python中的单引号,双引号和三双引号的区别

    1.单引号和双引号没有区别.都可以用就是为了方便,减少写太多的转义字符. 比如: str='Let\'s go!' str="Let's go!" 两种等价,但是第二种写法显然更优 ...

  6. 如果你使用WebView+FloatingActionButton

    在WebView中想要使用FAB,如果你想向上滑动的时候隐藏FAB,那么需要再WebView外面套一个ScrollView! 原因之前也分析过,和为什么ListView不能让ToolBar.Tab隐藏 ...

  7. spark提交异常日志分析

    java.lang.NoSuchMethodError: org.apache.spark.sql.SQLContext.sql(Ljava/lang/String;)Lorg/apache/spar ...

  8. 继承Application以实现全局资源共享

    原文地址:http://www.cnblogs.com/Dentist/p/Mr_Dentist_.html 每个程序运行时会创建一个Application类的对象且仅有一个.在app结束时这个App ...

  9. 使用svnadmin对VisualSVN进行项目迁移

    使用svnadmin对VisualSVN进行项目迁移 导出1> 启动命令行cmd2> 运行%VISUALSVN_SERVER%\bin\svnadmin dump PATH-TO-REPO ...

  10. C#读写.ini文件

    转载来源: http://blog.csdn.net/source0573/article/details/49668079 https://www.cnblogs.com/wang726zq/arc ...