以下是一个 Python 函数,用于找到序列中连续的相同元素序列:

def find_consecutive_sequence(seq):
    result = []
    current_sequence = [seq[0]]
    
    for i in range(1, len(seq)):
        if seq[i] == seq[i-1]:
            current_sequence.append(seq[i])
        else:
            result.append(current_sequence)
            current_sequence = [seq[i]]
    
    result.append(current_sequence)
    return result

这个函数接受一个序列作为参数(可以是列表、元组等),并返回一个包含连续相同元素序列的列表。例如,对于输入序列[1, 1, 2, 3, 3, 3, 4, 4, 5, 5, 5, 5],函数将返回[[1, 1], [3, 3, 3], [4, 4], [5, 5, 5, 5]]

你可以调用这个函数来查找任何序列中的连续相同元素序列。

Python 查找序列中连续相同元素序列的实现

原文地址: https://www.cveoy.top/t/topic/mKNc 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录