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.")
原创粉丝点击