String count with overlapping occurrences

前端 未结 22 3002
耶瑟儿~
耶瑟儿~ 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:50
    def count_substring(string, sub_string):
        count = 0
        for pos in range(len(string)):
            if string[pos:].startswith(sub_string):
                count += 1
        return count
    

    This could be the easiest way.

    0 讨论(0)
  • 2020-11-21 23:51
    def count_substring(string, sub_string):
        counter = 0
        for i in range(len(string)):
            if string[i:].startswith(sub_string):
            counter = counter + 1
        return counter
    

    Above code simply loops throughout the string once and keeps checking if any string is starting with the particular substring that is being counted.

    0 讨论(0)
  • 2020-11-21 23:51

    Function that takes as input two strings and counts how many times sub occurs in string, including overlaps. To check whether sub is a substring, I used the in operator.

    def count_Occurrences(string, sub):
        count=0
        for i in range(0, len(string)-len(sub)+1):
            if sub in string[i:i+len(sub)]:
                count=count+1
        print 'Number of times sub occurs in string (including overlaps): ', count
    
    0 讨论(0)
  • 2020-11-21 23:51

    A simple way to count substring occurrence is to use count():

    >>> s = 'bobob'
    >>> s.count('bob')
    1
    

    You can use replace () to find overlapping strings if you know which part will be overlap:

    >>> s = 'bobob'
    >>> s.replace('b', 'bb').count('bob')
    2
    

    Note that besides being static, there are other limitations:

    >>> s = 'aaa'
    >>> count('aa') # there must be two occurrences
    1 
    >>> s.replace('a', 'aa').count('aa')
    3
    
    0 讨论(0)
  • 2020-11-21 23:54
    def occurance_of_pattern(text, pattern):
        text_len , pattern_len = len(text), len(pattern)
        return sum(1 for idx in range(text_len - pattern_len + 1) if text[idx: idx+pattern_len] == pattern) 
    
    0 讨论(0)
  • 2020-11-21 23:57

    Python's str.count counts non-overlapping substrings:

    In [3]: "ababa".count("aba")
    Out[3]: 1
    

    Here are a few ways to count overlapping sequences, I'm sure there are many more :)

    Look-ahead regular expressions

    How to find overlapping matches with a regexp?

    In [10]: re.findall("a(?=ba)", "ababa")
    Out[10]: ['a', 'a']
    

    Generate all substrings

    In [11]: data = "ababa"
    In [17]: sum(1 for i in range(len(data)) if data.startswith("aba", i))
    Out[17]: 2
    
    0 讨论(0)
提交回复
热议问题