這應該是一個壓縮函數。我們應該只用文字閱讀文本文件,並按頻率和文字數量對它們進行排序。大寫和小寫。我沒有解決它的答案,我只是想要幫助。單詞的壓縮函數.. python
for each word in the input list
if the word is new to the list of unique words
insert it into the list and set its frequency to 1
otherwise
increase its frequency by 1
sort unique word list by frequencies (function)
open input file again
open output file with appropriate name based on input filename (.cmp)
write out to the output file all the words in the unique words list
for each line in the file (delimited by newlines only!)
split the line into words
for each word in the line
look up each word in the unique words list
and write its location out to the output file
don't output a newline character
output a newline character after the line is finished
close both files
tell user compression is done
這是我的下一個步驟:
def compression():
for line in infile:
words = line.split()
def get_file():
opened = False
fn = input("enter a filename ")
while not opened:
try:
infile = open(fn, "r")
opended = True
except:
print("Won't open")
fn = input("enter a filename ")
return infile
def compression():
get_file()
data = infile.readlines()
infile.close()
for line in infile:
words = line.split()
words = []
word_frequencies = []
def main():
input("Do you want to compress or decompress? Enter 'C' or 'D' ")
main()
如果你真的嘗試,告訴我們您的代碼,以及具體的問題你有。通常情況下,堆棧溢出將不會爲您完成全部功課。 – Amadan
請張貼您的最佳嘗試,我們將很樂意修復它。 – aIKid
是否有任何編號列表是問題規範的一部分,而不是您自己如何解決這個問題的想法?另外,你使用的是什麼版本的Python,並且你有使用標準庫中的類的限制嗎?我有一個簡單的解決方案,雖然正如你所要求的,我會給它的建議,而不僅僅是答案。 –