I have an Array of zeros and ones which looks like this:
mat = [0,0,0,1,1,0]
I need to write down a new array of positions where a zero/one starts and ends:
mat_help= [[0,3], [4,5], [6,6]]
I thought I could accomplish this Task by simply loop. But then I got stucked: how can I mark the beginning of each zero?
In principle, I've ended up with this Code which Looks ugly and where I still should add a nestedness to the final list:
mat=[0, 0, 0, 1, 1, 0, 0]
matrix_length = len(mat)
mat_help=[0]
for i in range(len(mat)-1):
if mat[i]==mat[i+1]:
continue
elif mat[i]!=mat[i+1]:
mat_help.append(i)
mat_help.append(i+1)
mat_help.append(matrix_length-1)
Maybe, there is a fast pythonic way of doing such things than just using several Loops...?
new_arrare 7 (from 0 to 6). Not sure if you know the problem statement very wellnew_arrcontains6as an index and yet it will raiseIndexErrorgiven the length of the list is 6.[0, 3], [3, 5], [5, 6]if the upper ends of the ranges are exclusive, or[0, 2], [3, 4], [5, 5]if they are inclusive, or[1, 3], [4, 5], [6, 6]if you start counting the indexes from 1, not 0 as is conventional. Please think more about what you want exactly, then the solution will probably follow naturally.