String count with overlapping occurrences

前端 未结 22 3099
耶瑟儿~
耶瑟儿~ 2020-11-21 23:25

What\'s the best way to count the number of occurrences of a given string, including overlap in Python? This is one way:

def function(string, str_to_search_f         


        
22条回答
  •  無奈伤痛
    2020-11-21 23:47

    For a duplicated question i've decided to count it 3 by 3 and comparing the string e.g.

    counted = 0
    
    for i in range(len(string)):
    
        if string[i*3:(i+1)*3] == 'xox':
           counted = counted +1
    
    print counted
    

提交回复
热议问题