String count with overlapping occurrences

前端 未结 22 3035
耶瑟儿~
耶瑟儿~ 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

    If you want to count permutation counts of length 5 (adjust if wanted for different lengths):

    def MerCount(s):
      for i in xrange(len(s)-4):
        d[s[i:i+5]] += 1
    return d
    

提交回复
热议问题