您可以在这里使用^{}。您仍然需要扫描字符串,但正则表达式也需要:from itertools import groupby
three_or_more = (char for char, group in groupby(input_string)
if sum(1 for _ in group) >= 3)
这将生成一个生成器;对其进行迭代以列出3次或更多次找到的所有字符,或者使用any()查看是否至少有一个这样的组:
^{pr2}$
不幸的是,re解决方案在这里更有效:>>> from timeit import timeit
>>> import random
>>> from itertools import groupby
>>> import re
>>> import string
>>> def consecutive_groupby(string):
... three_or_more = (char for char, group in groupby(string)
... if sum(1 for _ in group) >= 3)
... return any(three_or_more)
...
>>> def consecutive_re(string):
... return re.search(r'(.)\1\1', string) is not None
...
>>> # worst-case: random data with no cons