資源簡介
file=open("考研英語真題.txt",'r',encoding='UTF-8')
只要把文件放在同一目錄下,在上面這一行中改一下文件名就行。
考研時候感覺輔導書上的高頻詞匯有點問題,就寫了這么個程序^_^

代碼片段和文件信息
import?re
#coding=utf-8
file=open(“考研英語真題.txt“‘r‘encoding=‘UTF-8‘)
file_context=file.read()
#print?(file_context)
file.close()
print?(‘file?read?over?and?colsed‘)
print?(‘##‘*40)
word_dric={}
all_words_list=[]
all_words_list?=re.findall?(r‘.+?\b‘file_context)?#find?all?the?words?
#print?(all_words_list)
print?(‘find?all?the?words‘)
print?(‘##‘*40)
for?word?in?all_words_list:
????if?word?in?word_dric:
????????word_dric[word]=word_dric[word]+1
????else:?
????????word_dric[word]=1
print?(‘words?occurrences?count?over‘)
print?(‘##‘*40)
sortedlist?=sorted(word_dric.items()key?=?lambda?x:x[1]reverse?=?True)
#print?(sortedlist)
print(‘sorted?over‘)
print?(‘##‘*40)
file=open(“process_result.txt“‘w‘encoding=‘UTF-8‘)
for?item?in?sortedlist:
????file.write(str(item))
????file.write(‘\n‘)
file.close()
print?(‘result?write?into?txt?over‘)
‘‘‘
file=open(“process_result.txt“‘w‘encoding=‘UTF-8‘)
for?word?in?word_dric:
????file.write(word)
????file.write(‘???????:‘)
????file.write(?str(word_dric[word])?)
????file.write(‘\n‘)
file.close()
print?(‘result?write?over‘)
‘‘‘
?屬性????????????大小?????日期????時間???名稱
-----------?---------??----------?-----??----
?????文件????????1159??2019-01-21?12:43??count_words_occurrences.py
?????文件?????1083609??2019-01-21?03:34??考研英語真題.txt
- 上一篇:高效算法128例配套代碼
- 下一篇:Arcpy實現dem河網提取
評論
共有 條評論