import re
def findLongest(s):
try:
result = max(re.findall(r'((\w+?)\2+)', s), key=lambda t: len(t[0]))
except ValueError:
pass
else:
return result
mystr = "helloworld world world hellohellohelloworldworld"
print(findLongest(mystr))
Выводит
(output): ('hellohellohello', 'hello')
А нужно,чтобы вывело world, так как эта последовательность встречается чаще всего. Что не так?
Выражение должно искать вхождение, которое глобально повторяется наибольшее количество раз