I have a list/array(can make them arrays, doesn't matter):
array = [1,2,1,1,2,3,1,2,1,2,3,1,2,3,4,5]
I would like to create a new_array
FROM array
which looks like this:
new_array = [[1,2],[1],[1,2,3],[1,2],[1,2,3],[1,2,3,4,5]]
The logic I came up with was to start with a=0,b=1
and loop through array
, when the value of array[a] < array[b]
, then a+=1
,b+=1
, but if the value of array[a]>=array[b]
, then append the value of b
:
Here is what I tried:
index_1 = [] # This will be the list which will have the FIRST index
index_2 = [] # This will be the list which will have the SECOND index
a = 0
b = 1
for i in array:
if i[a] < i[b]:
a = a+1
b = b+1
elif i[a] >= i[b]:
index_2.append(b)
index_1.append(a)
So index_1
will have the first index and index_2
will have the second index and then, we can create the new_array
by:
new_array = [(array[start:end]) for start,end in zip(index_1,index_2)]
Unfortunately, even though my idea is correct, the loop stops in if i[a]<i[b]
because of IndexError: invalid index to scalar variable.
.