'Python fill array with values from index to another

I have 3 1D lists.

repetitions= [2, 1, 1, 3, 1, 1, 1, 1, 2, 1]
start = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
end = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110]

I want to fill a 2D output np.array with the value 1 from start[i] to end[i] using the repetitions list.

For example let's pretend my output array has a shape of (150,10) filled with zeros.

Output = [
         [0,0,0,0,0,0,0,0,0,0] #0
         [0,0,0,0,0,0,0,0,0,0] #1
         [0,0,0,0,0,0,0,0,0,0] #2
         [0,0,0,0,0,0,0,0,0,0] #3
         [0,0,0,0,0,0,0,0,0,0] #4
         [0,0,0,0,0,0,0,0,0,0] #5
         [0,0,0,0,0,0,0,0,0,0] #6
         [0,0,0,0,0,0,0,0,0,0] #7
         [0,0,0,0,0,0,0,0,0,0] #8
         [0,0,0,0,0,0,0,0,0,0] #9
         [0,0,0,0,0,0,0,0,0,0] #10
         [0,0,0,0,0,0,0,0,0,0] #11
         [0,0,0,0,0,0,0,0,0,0] #12
         [0,0,0,0,0,0,0,0,0,0] #13
         [0,0,0,0,0,0,0,0,0,0] #14
         ...
         ]

First I have to iterate the repetitions list to get the number of times to iterate start and end lists and to get the rows and columns index's to fill the output array.

For example, the first column of the repetitions list (the one with index 0) is equal to 2 then I will have to iterate start and end lists 2 times and fill output array, first, from the row index (start[0]==10) to the row end[0]==20 at the column index index 0 (which is the index of the repetition value 2 in the repetitions list) with the value 1.

Then from start[1] to end[2] at the index 0 also with the value 1.

The output array for the first iteration will be something like this :

Output = [
         [0,0,0,0,0,0,0,0,0,0] #0
         [0,0,0,0,0,0,0,0,0,0] #1
         [0,0,0,0,0,0,0,0,0,0] #2
         [0,0,0,0,0,0,0,0,0,0] #3
         [0,0,0,0,0,0,0,0,0,0] #4
         [0,0,0,0,0,0,0,0,0,0] #5
         [0,0,0,0,0,0,0,0,0,0] #6
         [0,0,0,0,0,0,0,0,0,0] #7
         [0,0,0,0,0,0,0,0,0,0] #8
         [0,0,0,0,0,0,0,0,0,0] #9
         [1,0,0,0,0,0,0,0,0,0] #10
         [1,0,0,0,0,0,0,0,0,0] #11
         [1,0,0,0,0,0,0,0,0,0] #12
         [1,0,0,0,0,0,0,0,0,0] #13
         [1,0,0,0,0,0,0,0,0,0] #14
         [1,0,0,0,0,0,0,0,0,0] #15
         [1,0,0,0,0,0,0,0,0,0] #16
         [1,0,0,0,0,0,0,0,0,0] #17
         [1,0,0,0,0,0,0,0,0,0] #18
         [1,0,0,0,0,0,0,0,0,0] #19
         [1,0,0,0,0,0,0,0,0,0] #20
         ...
         ]

Then the second column of the repetitions list (the one with index 1) is equal to 1 then I will have to iterate start and end lists 1 time and fill output array, from the row index (start[1]==20) to the row end[1]==30 at the column index index 1 (which is the index of the repetition value 1 in the repetitions list) with the value 1. And so on.

The output array for the second iteration will be something like this :

Output = [
         ...
         [1,0,0,0,0,0,0,0,0,0] #10
         [0,0,0,0,0,0,0,0,0,0] #11
         [0,0,0,0,0,0,0,0,0,0] #12
         [0,0,0,0,0,0,0,0,0,0] #13
         [0,0,0,0,0,0,0,0,0,0] #14
         [0,0,0,0,0,0,0,0,0,0] #15
         [0,0,0,0,0,0,0,0,0,0] #16
         [0,0,0,0,0,0,0,0,0,0] #17
         [0,0,0,0,0,0,0,0,0,0] #18
         [0,0,0,0,0,0,0,0,0,0] #19
         [0,1,0,0,0,0,0,0,0,0] #20
         [0,1,0,0,0,0,0,0,0,0] #21
         [0,1,0,0,0,0,0,0,0,0] #22
         [0,1,0,0,0,0,0,0,0,0] #23
         [0,1,0,0,0,0,0,0,0,0] #24
         [0,1,0,0,0,0,0,0,0,0] #25
         [0,1,0,0,0,0,0,0,0,0] #26
         [0,1,0,0,0,0,0,0,0,0] #27
         [0,1,0,0,0,0,0,0,0,0] #28
         [0,1,0,0,0,0,0,0,0,0] #29
         [0,1,0,0,0,0,0,0,0,0] #30
         ...
         ]

Here's what I have tried but I am confused :

Output=np.zeros((150, 10))

for i in range(len(start)):
    for j in range(10):
        ms = repetions[i][j]
        for p in range(ms):
            Output[start[i]:end[i], p]=1 


Solution 1:[1]

Without Numpy

repetitions= [2, 1, 1, 3, 1, 1, 1, 1, 2, 1]
start = [10, 20, 30, 40, 50, 60, 70, 80, 90, 100]
end = [20, 30, 40, 50, 60, 70, 80, 90, 100, 110]
x=10
imax=150
Output=[[0]*x]*imax
for i in range(len(start)):
    sample=[0]*x 
    sample[repetitions[i]]=1
    for p in range(end[i]-start[i]):
        Output[start[i]+p]=sample

Maybe you should have end[i]-start[i] in range

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1