2012-01-06 40 views
2

我正在嘗試創建一個函數,您可以在單詞「香蕉」中插入「ana」等短語,然後計算它找到該短語的次數在這個詞裏。我無法找到我爲某些測試單元所做的錯誤而無法工作。無法讓我的計數函數在Python中工作

def test(actual, expected): 
    """ Compare the actual to the expected value, 
     and print a suitable message. 
    """ 
    import sys 
    linenum = sys._getframe(1).f_lineno # get the caller's line number. 
    if (expected == actual): 
     msg = "Test on line {0} passed.".format(linenum) 
    else: 
     msg = ("Test on line {0} failed. Expected '{1}', but got '{2}'.".format(linenum, expected, actual)) 
    print(msg) 

def count(phrase, word): 
    count1 = 0 
    num_phrase = len(phrase) 
    num_letters = len(word)  

    for i in range(num_letters): 
     for x in word[i:i+num_phrase]: 
      if phrase in word: 
       count1 += 1 
      else: 
       continue  
     return count1 

def test_suite(): 
    test(count('is', 'Mississippi'), 2) 
    test(count('an', 'banana'), 2) 
    test(count('ana', 'banana'), 2) 
    test(count('nana', 'banana'), 1) 
    test(count('nanan', 'banana'), 0) 
    test(count('aaa', 'aaaaaa'), 4) 

test_suite() 
+1

什麼錯誤?附:請減少多餘的空行,讓您的問題更具可讀性。謝謝。 – 2012-01-06 13:53:20

+0

你在字[]中對x的迭代對我沒有意義。 – 2012-01-06 13:54:52

+0

你的變量名稱很混亂。例如'num_phrase'不是一個短語的數字,但是它的* length * * x'完全*是非描述性的。根據我的經驗,整理術語往往會在短期內發現問題。 – kindall 2012-01-06 19:32:22

回答

5

改變你count功能下面通過測試:

def count(phrase, word): 
    count1 = 0 
    num_phrase = len(phrase) 
    num_letters = len(word)  
    for i in range(num_letters): 
     if word[i:i+num_phrase] == phrase: 
      count1 += 1 
    return count1 
+0

謝謝。我不知道我是如何忽視這一點的。我想我只是讓我的功能太複雜了。 – user1091975 2012-01-06 14:10:01

+0

如果您正在搜索和/或在大字符串中有幾個[算法](http://en.wikipedia.org/wiki/String_searching_algorithm)加快搜索。 – MattH 2012-01-06 14:14:29

4

使用str.count(substring)。這將返回完整字符串中子字符串出現的次數(str)。

這裏是展示它是如何工作的交互式會話:

>>> 'Mississippi'.count('is') 
2 
>>> 'banana'.count('an') 
2 
>>> 'banana'.count('ana') 
1 
>>> 'banana'.count('nana') 
1 
>>> 'banana'.count('nanan') 
0 
>>> 'aaaaaa'.count('aaa') 
2 
>>> 

正如你看到的,功能非重疊。如果你需要重複的行爲,看看這裏:string count with overlapping occurrences

0

您使用迭代錯誤的,所以:

for i in range(num_letters): #This will go from 1, 2, ---> len(word)  

    for x in word[i:i+num_phrase]: 
    #This will give you the letters starting from word[i] to [i_num_phrase] 
    #but one by one, so : for i in 'dada': will give you 'd' 'a' 'd' 'a' 

     if phrase in word:  #This condition doesnt make sense in your problem, 
            #if it's true it will hold true trough all the 
            #iteration and count will be 
            #len(word) * num_phrase,     
            #and if it's false it will return 0 
      count1 += 1 
     else: 
      continue 
0

我猜測,str.count(substring)是錯誤的解決方案,因爲它不會計算重疊的子字符串,並且測試套件失敗。

也有內置str.find方法,這可能有助於該任務。

-1

這個時候有一個基本的問題。

當你看到一個字符串像"isisisisisi" howmany「isi」do you count?

在第一個狀態你看到字符串"isi s isi s isi"並返回3作爲計數。

在第二個狀態中,您將看到字符串"isisisisisi"並計算每個短語的「i」拖曳時間,如"isi isi isi isi isi"。 換句話說,第二'我'是第一'isi'的最後一個字符和第二'isi'的第一個字符。

所以你必須返回5作爲計數。

爲第一狀態簡直可以用:

>>> string = "isisisisisi" 
>>> string.count("isi") 
3 

和第二狀態,你必須認識到"phrase"+"anything"+"phrase"搜索關鍵字。

下面這個函數可以做到這一點:

def find_iterate(Str): 
    i = 1 
    cnt = 0 
    while Str[i-1] == Str[-i] and i < len(Str)/2: 
     i += 1 
     cnt += 1 
    return Str[0:cnt+1] 

現在你有很多選擇來計算字符串中的搜索關鍵字。

比如我做了這樣如下:

if __name__ == "__main__": 
    search_keyword = "isi" 
    String = "isisisisisi" 
    itterated_part = find_iterate(search_keyword) 
    c = 0 
    while search_keyword in String: 
     c += String.count(search_keyword) 
     String = String.replace(search_keyword, itterated_part) 
    print c 

我不知道是否有更好的方式是python.but我試圖用正則表達式的幫助,要做到這一點,但發現沒有辦法。

0

另一種方式:

高清計數(順序,項目):

count = 0 

    for x in sequence : 

    if x == item : 
    count = count+1 
    return count 
相關問題