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)