📜  python 检测列表中的范围 - Python 代码示例

📅  最后修改于: 2022-03-11 14:46:31.850000             🧑  作者: Mango

代码示例1
def detect_range(input_list):
    start = None
    length = 0

    for elem in input_list:

        # First element
        if start is None:
            start = elem
            length = 1
            continue

        # Element in row, just count up
        if elem == start + length:
            length += 1
            continue

        # Otherwise, yield
        if length == 1:
            yield start
        else:
            yield (start, start+length)

        start = elem
        length = 1

    if length == 1:
        yield start
    else:
        yield (start, start+length)


print(list(detect_range(a)))