2014-02-18 15 views
0

該程序的目的是讀取文件,將所有單詞轉換爲單個令牌,並將這些令牌放入數組中。該程序然後刪除所有標點並將所有字母改爲小寫。然後程序應該計算每個命令行參數在數組中出現的次數,並打印結果。我的程序能夠成功創建一個已刪除小寫令牌的數組。我現在的問題是如何遍歷數組並計算特定單詞的出現次數,以及如何在主函數中調用這些函數。我depunctuate功能可以作爲書面計算數組中項目的出現python

這是我的計劃:

import sys 
from scanner import * 

def main(): 
    print("the name of the program is",sys.argv[0]) 
    for i in range(1,len(sys.argv),1): 
     print(" argument",i,"is", sys.argv[i]) 
    tokens = readTokens("text.txt") 
    cleanTokens = depunctuateTokens(tokens) 
    words = [token.lower() for token in cleanTokens] 
    count = find(words) 
    print(words) 
    print(count) 
def readTokens(s): 
    arr=[] 
    s=Scanner("text.txt") 
    token=s.readtoken() 
    while (token != ""): 
     arr.append(token) 
     token=s.readtoken() 
    s.close() 
    return arr 

def depunctuateTokens(arr): 
    result=[] 
    for i in range(0,len(arr),1): 
     string=arr[i] 
     cleaned="" 
     punctuation="""!"#$%&'()*+,-./:;<=>[email protected][\]^_`{|}~""" 
     for i in range(0,len(string),1): 
      if string[i] not in punctuation: 
       cleaned += string[i] 
     result.append(cleaned) 
    return result 

def find(tokens,words): 
    return occurences(tokens,words)>0 

def occurences(tokens,words): 
    count = 0 
    for i in range(0,len(words),1): 
     if (words[i] == tokens): 
      count += 1 
     return count 

main() 
+1

[我如何計算Python中的列表項的出現?](http://stackoverflow.com/questions/2600191/how-can-i-count-the-occurrences-of-a- list-item-in-python) – isedev

+1

乍一看,比較[你以前的問題](http://stackoverflow.com/questions/21842427/mapping-a-function-over-all-the-letters-of-a -token-in-python),它看起來並不像你自己做了任何工作。你拿到了在那裏給你的代碼,然後詢問如何添加它來解決你的下一個家庭作業。如果這不正確,你應該閱讀[MCVE](http://stackoverflow.com/help/mcve)(正如我上次所說的),並給我們一個集中的例子,它只演示你需要編寫的部分,你到目前爲止已經嘗試過,以及你卡在哪裏。 – abarnert

+0

你說得對。對於那個很抱歉。生病讀了MCVE並刪除了這個問題。 – user3321218

回答

0

您現有的功能不是太離譜:

def occurences(tokens,words): 
    count = 0 
    for i in range(0,len(words),1): 
     if (words[i] == tokens): 
      count += 1 
     return count 

第一個問題是,你已經縮進for循環內的return count。這意味着它每次都會通過循環,這意味着它只會處理第一個單詞。所以,如果第一個單詞匹配,它將返回1,否則返回0。只是unindent return和這個問題消失。


的第二個問題是,通過參數的名字來看,你希望既tokenswords是字符串列表。所以,一個單詞words[i]永遠不會匹配整個令牌列表。也許你想測試這個詞是否與列表中的任何標記匹配,而不是它是否與列表匹配?在這種情況下,你會寫:

if words[i] in tokens: 

最後,當你find功能似乎調用occurences正確(當然,你拼寫occurrences錯了,但你這樣做是一致,所以沒關係),你實際上不要撥打find,所以你永遠不會到達這裏。您的電話是這樣的:

count = find(words) 

...但你的定義是這樣的:

def find(tokens,words): 

你必須東西傳遞給tokens參數。我不確定什麼傳遞 - 但你是設計和編寫此代碼的人;你寫了什麼功能?


我懷疑你是真的尋找的是每個令牌的計數。在這種情況下,在您的設計中,findoccurrences實際上應採用單個token,而不是tokens作爲參數的列表。在這種情況下你不要想要上面的in表達式,你要重命名這個參數。並且您沒有使用find,您只需直接撥打occurences即可。而你要調用它在一個循環中,像這樣:

for word in words: 
    count = occurences(word, words) 
    print('{}: {}'.format(word, count)) 

而且,就像你的另外兩個功能進行了重現已建成(str.translatelower)功能,這個人是太:list.count。如果您爲了學習的目的而自己編寫它,那很好,但如果這不是作業的一部分,只需使用內置函數即可。

2

使用list.count

>>> l = [1,2,3,4,5,6,7,44,4,4,4,4] 
>>> print(l.count(4)) 
>>> 5 
相關問題