相关疑难解决方法(0)

具有重叠事件的字符串计数

计算给定字符串出现次数的最佳方法是什么,包括python中的重叠?这是最明显的方式:

def function(string, str_to_search_for):
      count = 0
      for x in xrange(len(string) - len(str_to_search_for) + 1):
           if string[x:x+len(str_to_search_for)] == str_to_search_for:
                count += 1
      return count


function('1011101111','11')
returns 5
Run Code Online (Sandbox Code Playgroud)

或者在python中有更好的方法吗?

python string search

50
推荐指数
4
解决办法
5万
查看次数

标签 统计

python ×1

search ×1

string ×1