如何计算 Python 列表项中单词的连续最大出现次数
How to count Consecutive Maximum Occurrence of a word in an item of a list in Python
我正在尝试实现一个代码来计算列表项中最长的 运行 个单词
for_example = ['smaplakidfsmapsmapsmapuarebeautiful']
所以在这个例子中,它将是 3,因为 smap 被重复了 3 次所以有没有代码可以为我完成这个任务,不管这个词是什么。
编辑:
如果你有一个项目列表,你可以这样调用函数:
[countMaxConsecutiveOccurences(item, 'smap') for item in items]
def countMaxConsecutiveOccurences(item, s):
i = 0
n = len(s)
current_count = 0
max_count = 0
while i < len(item):
if item[i:i+n] == s:
current_count += 1
max_count = max(max_count, current_count)
i += n
else:
i += 1
current_count = 0
return max_count
countMaxConsecutiveOccurences('smaplakidfsmapsmapsmapuarebeautiful', 'smap')
我正在尝试实现一个代码来计算列表项中最长的 运行 个单词
for_example = ['smaplakidfsmapsmapsmapuarebeautiful']
所以在这个例子中,它将是 3,因为 smap 被重复了 3 次所以有没有代码可以为我完成这个任务,不管这个词是什么。
编辑: 如果你有一个项目列表,你可以这样调用函数:
[countMaxConsecutiveOccurences(item, 'smap') for item in items]
def countMaxConsecutiveOccurences(item, s):
i = 0
n = len(s)
current_count = 0
max_count = 0
while i < len(item):
if item[i:i+n] == s:
current_count += 1
max_count = max(max_count, current_count)
i += n
else:
i += 1
current_count = 0
return max_count
countMaxConsecutiveOccurences('smaplakidfsmapsmapsmapuarebeautiful', 'smap')