课后作业3

20170****7253  董昕诚

码云地址:https://gitee.com/dxcaly/word_frequency

 d = open(dst)
except IOError as s:
print (s)
打开文件只读模式并调试输出序列化
    bvffer = d.read()
except:
print ("Read File Error!")
return None
d.close()
转换成bvffer类并序列化
# 下面添加处理缓冲区 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]: # 输出 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)
name赋值給main执行以下进行修改语句
if word in word_freq.keys():
word_freq[word] += 1
else:
word_freq[word] = 1

return word_freq

修改删掉.keys使其不再浪费多余进程时间

 

 

 

 

 


 

 

 

 

 

 

 

 

 

 

 

 

 

转载于:https://www.cnblogs.com/dxc12581/p/10645893.html

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值