101个python小代码 (9)词频统计

本文分析了一篇文本中出现频率较高的词汇,如'the', 'a', 'of'等,并揭示了与信息技术相关的关键词,如'SCP-6599'、'Java'等。通过词频统计,展示了不同领域的子话题,如前端开发、后端开发和自然语言处理。
摘要由CSDN通过智能技术生成

代码:

import string
path = 'D:/桌面/wiki.txt'
with open(path,'r',encoding="utf-8") as text:
    words = [raw_word.strip(string.punctuation).lower() for raw_word in text.read().split()]
    words_index = set(words)
    counts_dict = {index:words.count(index) for index in words_index}
for word in sorted(counts_dict,key=lambda x: counts_dict[x],reverse=True):
    print('{} -- {} times'.format(word,counts_dict[word]))

运行结果:

the -- 58 times  a -- 40 times of -- 36 times  and -- 29 times  to -- 28 times  in -- 21 times
 -- 18 times  an -- 16 times  that -- 15 times  scp-6599 -- 14 times  i -- 14 times
is -- 14 times  with -- 13 times  from -- 12 times  you -- 12 times  by -- 10 times
scp-6599-1 -- 10 times  was -- 9 times  account -- 9 times  for -- 9 times
are -- 8 times  it -- 8 times  on -- 7 times  have -- 7 times  been -- 7 times
or -- 7 times  all -- 6 times  has -- 6 times  following -- 6 times  about -- 6 times
03/28/2008 -- 6 times  mon -- 6 times  your -- 6 times  muppet -- 6 times
hogslice -- 6 times  accounts -- 6 times  dont -- 5 times  this -- 5 times
alt-f4 -- 5 times  event -- 5 times  no -- 5 times  as -- 5 times  do -- 5 times
what -- 5 times  posts -- 4 times  puppets -- 4 times  entity -- 4 times
they -- 4 times  gregthecarp -- 4 times  users -- 4 times  shit -- 4 times
mothman -- 4 times  forum -- 4 times  below -- 4 times  class -- 4 times
website -- 4 times  it's -- 4 times  like -- 4 times  thread -- 4 times
central -- 4 times  my -- 4 times  when -- 3 times  under -- 3 times
......

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值