Horje
python detect ranges in list Code Example
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)))




Python

Related
flask socketio with gevent Code Example flask socketio with gevent Code Example
torch.nan_to_num Code Example torch.nan_to_num Code Example
how to read comment before the root element of xml python Code Example how to read comment before the root element of xml python Code Example
lineplot in plt Code Example lineplot in plt Code Example
Username and Password Login Function Code Example Username and Password Login Function Code Example

Type:
Code Example
Category:
Coding
Sub Category:
Code Example
Uploaded by:
Admin
Views:
8