Assign value to an array based on a list with indices

Viewed 36

I have an numpy array in Python (67x1440) of zeros that represents 67 vehicles for 1440 minutes (one day). I also have a list (67x2) of values when these vehicles are charging, one column with the minute for when the vehicle starts charging and one when the vehicles stops charging. See the table below (stop is always larger than start).

I would like to create an 2D (67x1440) array with zeros everywhere except when a vehicle is charging (filling in the values between start and stop with ones). Generally, I think the question can be formulated as that I would like to replace values in an existing array given that I have a list of indices with the locations to replace the values. Preferably I would like do to this without a for loop.

Start charging Stop charging
30 80
100 200
75 124
843 943
304 363
1 Answers

What's wrong with a for-loop? It'll only require one loop so it won't be that inefficent.

new_array = np.zeros(67, 1440)
for charge_time in charge_time_array:
    new_array[charge_time[0]:charge_time[1]] = 1
Related