python __detect 连续重复的字符串

python __detect continuous repeated string

我有这个字符串

"hellooooo my name is ooortda boooo"

detect_rept_str("o")

输出将是一个字符串的所有计数 连续重复多次。

all_dtct=[5,3,4]

"my string".count() 不适合我的需要。

这行得通吗?

def count_adjacent_repeats(haystack, needle):
    if '' in (haystack, needle) or needle not in haystack:
        return []

    counts = []
    for s in haystack.split(needle)[:-1]:
        if s == '':
            if counts == []:
                counts.append(0)
            counts[-1] += 1
        else:
            counts.append(1)

    return counts