String count with overlapping occurrences String count with overlapping occurrences python python

String count with overlapping occurrences


Well, this might be faster since it does the comparing in C:

def occurrences(string, sub):    count = start = 0    while True:        start = string.find(sub, start) + 1        if start > 0:            count+=1        else:            return count


>>> import re>>> text = '1011101111'>>> len(re.findall('(?=11)', text))5

If you didn't want to load the whole list of matches into memory, which would never be a problem! you could do this if you really wanted:

>>> sum(1 for _ in re.finditer('(?=11)', text))5

As a function (re.escape makes sure the substring doesn't interfere with the regex):

>>> def occurrences(text, sub):        return len(re.findall('(?={0})'.format(re.escape(sub)), text))>>> occurrences(text, '11')5


You can also try using the new Python regex module, which supports overlapping matches.

import regex as redef count_overlapping(text, search_for):    return len(re.findall(search_for, text, overlapped=True))count_overlapping('1011101111','11')  # 5