使用tuple统计文件中单词的个数
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统计文件中单词的个数的更多相关文章
- 统计文件中单词的个数---Shell及python版
最近在看shell中有个题目为统计单词的个数,使用了awk功能,代码如下 #!/bin/bash ];then echo "Usage:basename $0 filename" ...
- 学c语言做练习之统计文件中字符的个数
统计文件中字符的个数(采用命令行参数) #include<stdio.h> #include<stdlib.h> int main(int argc, char *argv[] ...
- JAVA实验--统计文章中单词的个数并排序
分析: 1)要统计单词的个数,就自己的对文章中单词出现的判断的理解来说是:当出现一个非字母的字符的时候,对前面的一部分字符串归结为单词 2)对于最后要判断字母出现的个数这个问题,我认为应该是要用到ma ...
- C语言算法--统计字符串中单词的个数
#include <stdio.h> #include <string.h> #include <stdlib.h> int main(void) { int le ...
- Python 统计文本中单词的个数
1.读文件,通过正则匹配 def statisticWord(): line_number = 0 words_dict = {} with open (r'D:\test\test.txt',enc ...
- Linux统计文件中单词出现的次数
grep -E "\b[[:alpha:]]+\b" /etc/fstab -o | sort | uniq -c 或 awk '{for(i=1;i<NF;i++){c ...
- Scala快速统计文件中特定单词,字符的个数
val fileContent=Source.fromFile("/home/soyo/桌面/ss5.txt").getLines.mkString(",") ...
- sort +awk+uniq 统计文件中出现次数最多的前10个单词
实例cat logt.log|sort -s -t '-' -k1n |awk '{print $1;}'|uniq -c|sort -k1nr|head -100 统计文件中出现次数最多的前10个单 ...
- java统计文本中单词出现的个数
package com.java_Test; import java.io.File; import java.util.HashMap; import java.util.Iterator; imp ...
随机推荐
- 英语发音规则---Z字母
英语发音规则---Z字母 一.总结 一句话总结:字母Z的名称zed /zed/,美式英语的称zee /zi:/,少数方言(如香港)读izzard /'izəɹd/. 1.字母Z在单词中发[z]? pu ...
- for循环中删除map中的元素,valgrind检测提示error:Invalid read of size 8
#include <iostream> #include <map> using namespace std; class A { public: typedef std::m ...
- Ueditor--toolbars
(1)代码中定义 <script id="container" name="content" type="text/plain"> ...
- 机器学习(十九)— xgboost初试kaggle
1.官网下载kaggle数据集Homesite Competition数据集,文件结构大致如下: 2.代码实战 #Parameter grid search with xgboost #featur ...
- Python基础-网络编程request使用
import requests#get请求 url = "http://127.0.0.1:8000/login" data={"username":" ...
- BEC listen and translation exercise 43
Reach for the stars so if you fall you land on a cloud.飞向星空吧,就算坠落,接住你的也是云彩. Anyway, exam failure can ...
- MFC动态创建
每个继承自CObject的对象并不会有与之对应的CRuntimeClass与之对应,除非使用了宏DECLARE_DYNAMIC\DECLARE_DYNCREATE\DECLARE_SERIAL. 这三 ...
- Android开发中高效的数据结构
android开发中,在java2ee或者android中常用的数据结构有Map,List,Set,但android作为移动平台,有些api(很多都是效率问题)显然不够理想,本着造更好轮子的精神,an ...
- 如果你使用WebView+FloatingActionButton
在WebView中想要使用FAB,如果你想向上滑动的时候隐藏FAB,那么需要再WebView外面套一个ScrollView! 原因之前也分析过,和为什么ListView不能让ToolBar.Tab隐藏 ...
- redis多机集群部署文档
redis多机集群部署文档(centos6.2) (要让集群正常工作至少需要3个主节点,在这里我们要创建6个redis节点,其中三个为主节点,三个为从节点,对应的redis节点的ip和端口对应关系如下 ...