python How to replace the first f(x) values of each x in array(x,n)

Viewed 725

I would like to replace the first x values in every row of my array a with ones and to keep all the other values NaN. The first x values however changes in every row and is determined by a list b.

Since I'm not very familiar with arrays I thought I might do this with a for loop as shown below, but this doesn't work (I've got inspiration for the basics of replacement in arrays from How to set first N elements of array to zero?).

In:
a = np.empty((3,4))
a.fill(np.nan)
b = [2,3,1]

for i in range(b):
    a[0:b[i]] = [1] * b[i] 
    a[i:] = np.ones((b[i]))
pass

Out:
line 7:
ValueError: could not broadcast input array from shape (2) into shape (2,4)

Result should be like:

Out:
[[1, 1, nan, nan], 
 [1, 1, 1, nan], 
 [1, nan, nan, nan]] 
4 Answers
Related