Search
 
SCRIPT & CODE EXAMPLE
 
CODE EXAMPLE FOR PYTHON

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)))
Source by stackoverflow.com #
 
PREVIOUS NEXT
Tagged: #python #detect #ranges #list
ADD COMMENT
Topic
Name
9+8 =