Answers for "python detect ranges in list"

0

python detect ranges in list

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)))
Posted by: Guest on December-14-2020

Code answers related to "python detect ranges in list"

Python Answers by Framework

Browse Popular Code Answers by Language