【python 用,。将文本进行分割】

在这里插入图片描述
python二级历年经典第15题的第7道
看着结果没有问题,就是线上评卷不通过

import jieba
dict_words = {}
list1 =[]
list2 =[]
highestWord =""
txt=""
content=""
outputtxt=""
with open('data3.txt', 'r', encoding='GBK') as f:
    #找到出现频次最高的词语
    list1=jieba.lcut(f.read())
    for i in list1:
        if len(i) >=2:
            dict_words[i]=dict_words.get(i,0)+1
    list1= list(dict_words.items())
    list1.sort(key=lambda s:s[1],reverse=True)
    highestWord =list1[0][0]
    #将txt内容进行分割
    f.seek(0)
    content=f.read()
    for m in content:
        if m !="。" and m !=",":   #注意是and,不可以是or
            txt=txt+m
        else:
            list2.append(txt)
            txt=""
    #print(list2)
    #找出包含最高词频的句子
    for j in list2:
        j=j.replace("\n","")
        if highestWord in j:
            outputtxt = outputtxt+j+"\n"
with open('out.txt', 'w', encoding='GBK') as fo:
    fo.write(outputtxt)
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值