String count with overlapping occurrences

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

    My answer, to the bob question on the course:

    s = 'azcbobobegghaklbob'
    total = 0
    for i in range(len(s)-2):
        if s[i:i+3] == 'bob':
            total += 1
    print 'number of times bob occurs is: ', total
    
    0 讨论(0)
  • 2020-11-21 23:45

    That can be solved using regex.

    import re
    def function(string, sub_string):
        match = re.findall('(?='+sub_string+')',string)
        return len(match)
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 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
    
    0 讨论(0)
  • 2020-11-21 23:49
    >>> 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
    
    0 讨论(0)
  • 2020-11-21 23:50

    How to find a pattern in another string with overlapping

    This function (another solution!) receive a pattern and a text. Returns a list with all the substring located in the and their positions.

    def occurrences(pattern, text):
        """
        input: search a pattern (regular expression) in a text
        returns: a list of substrings and their positions 
        """
        p = re.compile('(?=({0}))'.format(pattern))
        matches = re.finditer(p, text)
        return [(match.group(1), match.start()) for match in matches]
    
    print (occurrences('ana', 'banana'))
    print (occurrences('.ana', 'Banana-fana fo-fana'))
    

    [('ana', 1), ('ana', 3)]
    [('Bana', 0), ('nana', 2), ('fana', 7), ('fana', 15)]

    0 讨论(0)
提交回复
热议问题