📌  相关文章
📜  Python – 最大连续子串出现次数

📅  最后修改于: 2022-05-13 01:55:01.811000             🧑  作者: Mango

Python – 最大连续子串出现次数

有时,在使用Python时,我们可能会遇到需要检查连续重复出现的子字符串的问题。这可以在数据域中应用。让我们讨论一种可以执行此任务的方式。

方法:使用max() + re.findall()
上述方法的组合可用于执行此任务。在此,我们使用 findall() 提取重复的子字符串,并使用 max() 提取其中的最大值。

# Python3 code to demonstrate working of 
# Maximum Consecutive Substring  Occurrence
# Using max() + re.findall()
import re
  
# initializing string
test_str = 'geeksgeeks are geeks for all geeksgeeksgeeks'
  
# printing original string
print("The original string is : " + str(test_str))
  
# initializing subs 
sub_str = 'geeks'
  
# Maximum Consecutive Substring  Occurrence
# Using max() + re.findall()
res = max(re.findall('((?:' + re.escape(sub_str) + ')*)', test_str), key = len)
  
# printing result 
print("The maximum run of Substring : " + res) 
输出 :
The original string is : geeksgeeks are geeks for all geeksgeeksgeeks
The maximum run of Substring : geeksgeeksgeeks