2013-12-11 29 views
0

這應該是一個壓縮函數。我們應該只用文字閱讀文本文件,並按頻率和文字數量對它們進行排序。大寫和小寫。我沒有解決它的答案,我只是想要幫助。單詞的壓縮函數.. 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() 
+3

如果你真的嘗試,告訴我們您的代碼,以及具體的問題你有。通常情況下,堆棧溢出將不會爲您完成全部功課。 – Amadan

+0

請張貼您的最佳嘗試,我們將很樂意修復它。 – aIKid

+0

是否有任何編號列表是問題規範的一部分,而不是您自己如何解決這個問題的想法?另外,你使用的是什麼版本的Python,並且你有使用標準庫中的類的限制嗎?我有一個簡單的解決方案,雖然正如你所要求的,我會給它的建議,而不僅僅是答案。 –

回答

1

所以我不會爲你做一個完整的任務,但我可以盡我所能,並引導您完成一個接一個。

看起來第一步是創建文本文件中所有單詞的數組(假設您知道文件讀取方法)。爲此,您應該看看Python的split函數(regular expressions可用於此更復雜的變體)。因此,您需要將每個單詞存儲在某個位置,並將該值與其出現的次數配對。聽起來像dictionary的工作,對我來說。這應該讓你走上正軌。

1

感謝僞代碼,我可以或多或少地弄清楚這應該是什麼樣子。我不得不做一些猜測,因爲你說你受限於你在課堂上所講的內容,而我們無法知道包括什麼,什麼不包含。

我假設你可以處理打開輸入文件並將其拆分爲單詞。這是非常基本的東西 - 如果你的班級要求你處理輸入文件,它必須覆蓋它。如果沒有,回顧一下主要的事情是,你可以遍歷一個文件,並得到其行:

with open('example.txt') as infile: 
    for line in infile: 
     words = line.split() 

現在,對於每一個你需要保持的兩件事情軌道字 - 詞本身和它的頻率。您的問題需要您使用列表來存儲您的信息。這意味着您必須使用兩個不同的列表,一個存儲單詞並存儲它們的頻率,或者使用一個列表爲每個位置存儲兩個事實。這兩種方式都有缺點 - 列表並不是用於此的最佳數據結構,但是您的問題定義現在將更好的工具放在極限之外。

我可能會使用兩個不同的列表,一個包含單詞,一個包含單詞列表中同一位置的單詞的頻率計數。這樣做的好處是可以使用單詞列表上的index方法查找給定單詞的列表位置,然後遞增匹配的頻率計數。這將比使用for循環搜索存儲單詞和頻率計數的列表快得多。不利的一面是,排序更困難 - 您必須找到一種方法,在排序時保留每個頻率的列表位置,或者在排序之前合併單詞及其頻率。在這種方法中,您可能需要構建一個列表數據,該數據存儲兩條信息 - 頻率計數,然後是單詞列表索引或單詞本身 - 然後按頻率計數對該列表進行排序。

我希望這個練習的一部分內容是幫助您在家中使用它們時更好的數據結構是多麼有用。

編輯 所以內環將會是這個樣子:

words = [] 
word_frequencies = [] 
for line in infline: 
    for word in line.split(): 
     try: 
      word_position = words.index(word) 
     except ValueError: 
      # word is not in words 
      words.append(word) 
      # what do you think should happen to word_frequencies here? 
     else: 
      # now word_position is a number giving the position in both words and word_frequencies for the word 
      # what do you think should happen to word_frequences here? 
+0

好的,現在我已經分開了單詞。我想我需要初始化兩個單獨的列表。但我不知道他們需要多長時間......如此...... wordcount = [0] * len(文字)或類似的東西? – user3089144

+0

我不太清楚「初始化」是什麼意思 - 如果你使用兩個匹配的列表,我會指望看到'words = []'和'word_frequencies = []''等聲明。如果您在前面不知道它們,則不必在Python中預設列表長度 - 「append」等將基本上無限期地工作。我並不確定究竟是在典型系統上的極限位置,但它非常非常高。如果你確實瞭解它們,那麼在某些情況下它可能是一種優化,但如果你不這樣做,則不值得擔心。 –

+0

嗯,在這裏我們是:http://stackoverflow.com/questions/855191/how-big-can-a-python-array-get - 在任何情況下,預分配都不會改變這種情況,這只是性能上的改進其中它很重要。大多數情況下,你可以不擔心它。 –