python\uuu检测连续重复字符串

2024-09-28 03:19:30 发布

您现在位置:Python中文网/ 问答频道 /正文

我有这根绳子

"hellooooo my name is ooortda boooo"

detect_rept_str("o")

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

all_dtct=[5,3,4]

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


Tags: 字符串namestringismyall计数detect
1条回答
网友
1楼 · 发布于 2024-09-28 03:19:30

这样行吗

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

相关问题 更多 >

    热门问题