jeudi 3 juin 2021

Printing subsequent sublist python

I would like to print a subsequent sublist given some conditions. As an example, the condition is writing sublist with subsequent numbers for example:

arr = [ 1, 2, 2, 3,6,9,10,11,12 ]
arrlis = []
       
for i in range(len(arr)):
    if (arr[i] == arr[i - 1] + 1):
        arrlis.append(arr[1:i+count])
        count += 1
    else:
        count = 1
print(arrlis)
             

with the output:

[[2], [2, 2, 3], [2, 2, 3, 6, 9], [2, 2, 3, 6, 9, 10], [2, 2, 3, 6, 9, 10, 11, 12]]
    
       

but I would like to have the output

 [[2, 2, 3],[9, 10, 11, 12]]

The problem is that I am not sure how to use the conditional to append the output in different list. Does anyone have a clue how to do this?. Thanks in advance.

Aucun commentaire:

Enregistrer un commentaire