Python二级--政府报告分词-1

政府报告分词


题目一:
概述:


'''
问题1:数据统计。要求:修改PY301-1. py文件中代码,分别统计两个文件中出现次数最多的10词语,作为主题
词,要求词语不少于2个字符,打印输出在屏幕上,输出示例如下: (示例词语非答案 )
2019:改革:10,企业:9, .. (略),深化:2
2018:改革:11,效益:7, .. (略),深化:1
注意:输出格式采用英文冒号和英文逗号,标点符号前后无空格,各词语间用逗号分隔,最后一个词语后无逗
号。
'''

思路:

  1. 因为两个文本信息的处理方式都一样,所以定义一个函数来进行操作(偷偷懒)
def fun(txt):
	pass
  1. 读取所有文件
    fp = open(txt)
    res = fp.read()
    words = jieba.lcut(res)
  1. 词频统计并排序
  d = {}
  for word in words:
      if len(word) >= 2:
          d[word] = d.get(word, 0) + 1
lt = list(d.items())
lt.sort(key=lambda x: x[1], reverse=True)
  1. 获取参数的年份
 # 获取参数的年份
tmp = txt[txt.find('2'):txt.find('.')] + ':'
  1. 输出结果
for i in range(10):
    tmp += '{}:{},'.format(lt[i][0], lt[i][-1])
print(tmp[:-1])
  1. 释放资源
# 关闭文件释放资源
fp.close()

**总结**:
没什么说的,基本套路
代码如下:

import jieba


def fun(txt):
    fp = open(txt)
    res = fp.read()
    words = jieba.lcut(res)
    # print(words)
    d = {}
    for word in words:
        if len(word) >= 2:
            d[word] = d.get(word, 0) + 1
    lt = list(d.items())
    lt.sort(key=lambda x: x[1], reverse=True)
    # 获取参数的年份
    tmp = txt[txt.find('2'):txt.find('.')] + ':'

    # 输出结果
    for i in range(10):
        tmp += '{}:{},'.format(lt[i][0], lt[i][-1])
    print(tmp[:-1])

    fp.close()
if __name__ == '__main__':
    fun('data2019.txt')
    fun('data2018.txt')

相关代码和资源都会打包到下面的链接(另附一个份刷题笔记):
刷题经验
代码(直接用Python导入即可),软件,题库:
链接:https://pan.baidu.com/s/1WClgPe1D79_GKclR26LJdA
提取码:pjmm

  • 1
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 2
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

China@V

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值