pythonnumpy

How can I partially fill a numpy array with a value given a range?


Given a numpy array, a range, and a value, how can I fill the array with the value inside the range?

Is there a way that is faster than filling it in manually with a loop one by one?

Edit:

myArray = np.zeros(10)

What I want: [0, 0, 0, 0, 1, 1, 1, 0, 0, 0]


Solution

  • arr = np.zeros(10)
    arr[4:7] = 1
    print(arr)
    

    Output:

    array([0., 0., 0., 0., 1., 1., 1., 0., 0., 0.])