个人信息
- 李朋举
- 2017*****7173
-
程序分析
读文件到缓冲区
def process_file(dst): try: # 打开文件 l = open(dst, 'r') except IOError as s: print (s) return None try: # 读文件到缓冲区 bvffer=l.read() except: print ("Read File Error!") return None l.close()return bvffer
统计单词频率
def process_buffer(bvffer): if bvffer: word_freq = {} # 下面添加处理缓冲区 bvffer代码,统计每个单词的频率,存放在字典word_freq for i in bvffer.split(): word1 = i.strip(punctuation + " ") if word1 in word_freq: word_freq[word1] += 1 else: word_freq[word1] = 1 return word_freq输出Top10单词
def output_result(word_freq): if word_freq: sorted_word_freq = sorted(word_freq.items(), key=lambda v: v[1], reverse=True) for item in sorted_word_freq[:10]: # 输出 Top 10 的单词 print(item)主函数
if name == "main": import argparse parser = argparse.ArgumentParser() parser.add_argument('dst') args = parser.parse_args() dst = args.dst bvffer = process_file(dst) word_freq = process_buffer(bvffer) output_result(word_freq)
性能分析
执行次数最多:word1 = i.strip(punctuation + " ")
执行时间最长:ocess_buffer(bvffer):函数程序运行结果截图
总结与反思
本次学习运用到了python的结构性技巧,很充实!!!