无法让我的计数函数在Python中工作

问题描述:

我正在尝试创建一个函数,您可以在单词“香蕉”中插入“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

改变你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

使用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

您使用迭代错误的,所以:

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 

我猜测,str.count(substring)是错误的解决方案,因为它不会计算重叠的子字符串,并且测试套件失败。

也有内置str.find方法,这可能有助于该任务。

这个时候有一个基本的问题。

当你看到一个字符串像"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我试图用正则表达式的帮助,要做到这一点,但发现没有办法。

另一种方式:

高清计数(顺序,项目):

count = 0 

    for x in sequence : 

    if x == item : 
    count = count+1 
    return count