Python Word Count
来源:互联网 发布:手机淘宝怎样创桌面 编辑:程序博客网 时间:2024/06/01 07:48
Word Count , 体现了一些python的细节
# -*- coding: utf-8 -*-import os, sys, string, time, operatorPROJDIR = os.path.abspath(os.path.dirname(__file__)) + "/"inputFile = ""outputWordNumber = 0wordCount = {}if __name__ == "__main__": start_time = time.time() inputFile = "input.txt" outputWordNumber = 10 f = open(PROJDIR + inputFile, encoding='utf-8') lines = f.readlines() f.close() # read each line of the input file for line in lines: # remove punctuation # python2: line = line.translate(str.maketrans('', '') , string.punctuation) # str.translate 和 unicode.translate是两个不同的方法 line = line.translate(str.maketrans('', '', string.punctuation)) # split the line by space and get a word array wordArray = line.split() # build the dictionary to count each word for word in wordArray: word = word.lower() if word in wordCount: wordCount[word] = wordCount[word] + 1 else: wordCount[word] = 1 # sort the dictionary sorted_wordCount = sorted(wordCount.items(), key=operator.itemgetter(1), reverse=True) # print out the results i = 0 for key, value in sorted_wordCount: if i < outputWordNumber: print(key, value) i = i + 1 else: break # get the elapsed time elapsed_time = time.time() - start_time print("\nDone! Elapsed time: ", elapsed_time, " seconds.")
阅读全文
1 0
- python word count
- Python Word Count
- [Python]用正则表达式进行word Count
- Word Count
- Python+Hadoop Streaming实现MapReduce(word count)
- Python+Hadoop Streaming实现MapReduce(word count)
- [Memo] restricted word count
- hadoop word count example
- Word Count on hadoop
- Word Count示例
- Spark word count 实例
- Scala 版 word count
- Spark-Word Count实例
- Spark---Word Count
- spark word count
- Word Count (Map Reduce)
- Word Count (Map Reduce)
- Python count()
- XSLT学习(八)apply-templates
- 编译OSG源码并且配置使用
- java编程思想学习日志——对象导论
- redis 报错 Redis protected-mode 配置文件没有真正启动
- 【重要】XSLT学习(九)通过JavaScript转化xml
- Python Word Count
- 【JZOJ 1319】邮递员(欧拉回路)
- 机器学习笔记一:线性回归
- Httpclient写爬虫
- XSLT学习(十)服务器上跨浏览器解决方案(解决上一章第九章的遗留问题)
- 【bzoj2506】calc
- 查看和调试Qt源码
- 机构自由度的计算例子
- 次短路 poj3255 Roadblocks