Search
 
SCRIPT & CODE EXAMPLE
 

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)))
Comment

PREVIOUS NEXT
Code Example
Python :: 5.2.5: Counting 10 to 100 by ... 
Python :: sneon dr pepper 
Python :: python pyhue 
Python :: how to return value in new record to odoo 
Python :: vscode python region folding 
Python :: python print top 5 results of array 
Python :: poision in chinese 
Python :: pyqgis 
Python :: python multiline code dot 
Python :: tf.io path copy 
Python :: python time modülü 
Python :: make_interp_spline 
Python :: split dat file into datafram in python 
Python :: python script superuser 
Python :: csv logger keras 
Python :: 2d grid python pygame 
Python :: difference in django project view and app view 
Python :: concatenate dataframes using one column 
Python :: emacs shift region left python 
Python :: what is mi casa in spanish 
Python :: PyQT5 reset color 
Python :: create a dict from variables and give name 
Python :: numpy collapse last dimension 
Python :: python coule nod import the module virtualenvwrapper.hook_loader 
Python :: pyttsx python not working 
Python :: python writelignes 
Python :: python list comprehension with filter example 2 
Python :: how to pull images from android device from usb in python 
Python :: if self.quitting: raise BdbQuit in classbased view 
Python :: change alignment of selenium window 
ADD CONTENT
Topic
Content
Source link
Name
1+7 =