其他分享
首页 > 其他分享> > 第三次课后作业

第三次课后作业

作者:互联网

 

 

姓名:魏常荣 

学号:2017035107057

码云地址:https://gitee.com/yh-1101/third_homework.git

from string import punctuation
def process_file(dst):
try:
f = open(dst)
except IOError as s:
print (s)
return None
try:
bvffer = f.read()
except:
print ("Read File Error!")
return None
f.close()
return bvffer

def process_buffer(bvffer):
if bvffer:
word_freq = {}
for item in bvffer.strip().split():
word = item.strip(punctuation+' ')
if word in word_freq.keys():
word_freq[word] += 1
else:
word_freq[word] = 1
return word_freq

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]:
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)

改进前效果

改进前总时长

改进前占用时间内存最多的东西

改进后效果

改进后总时长

改进后占用时间内存最多的东西

通过此次作业对python有了更深的认识和了解。

 

标签:__,word,第三次,process,dst,作业,bvffer,课后,freq
来源: https://www.cnblogs.com/yh-1101/p/10666659.html