Count number of occurrences of a given substring in a string

后端 未结 30 1689
不思量自难忘°
不思量自难忘° 2020-11-22 13:58

How can I count the number of times a given substring is present within a string in Python?

For example:

>>> \'foo bar foo\'.numberOfOccurre         


        
30条回答
  •  太阳男子
    2020-11-22 14:57

    I will keep my accepted answer as the "simple and obvious way to do it" - however, that does not cover overlapping occurrences. Finding out those can be done naively, with multiple checking of the slices - as in: sum("GCAAAAAGH"[i:].startswith("AAA") for i in range(len("GCAAAAAGH")))

    (which yields 3) - it can be done by trick use of regular expressions, as can be seen at Python regex find all overlapping matches? - and it can also make for fine code golfing - This is my "hand made" count for overlappingocurrences of patterns in a string which tries not to be extremely naive (at least it does not create new string objects at each interaction):

    def find_matches_overlapping(text, pattern):
        lpat = len(pattern) - 1
        matches = []
        text = array("u", text)
        pattern = array("u", pattern)
        indexes = {}
        for i in range(len(text) - lpat):
            if text[i] == pattern[0]:
                indexes[i] = -1
            for index, counter in list(indexes.items()):
                counter += 1
                if text[i] == pattern[counter]:
                    if counter == lpat:
                        matches.append(index)
                        del indexes[index]
                    else:
                        indexes[index] = counter
                else:
                    del indexes[index]
        return matches
    
    def count_matches(text, pattern):
        return len(find_matches_overlapping(text, pattern))
    

提交回复
热议问题