I have a long array:
x= ([2, 5, 4, 7, ...])
for which I need to set the first N elements to 0. So for N = 2, the desired output would be:
x = ([0, 0, 4, 7, ...])
Is there an easy way to do this in Python? Some numpy function?
Pure python:
x[:n] = [0] * n
with numpy:
y = numpy.array(x)
y[:n] = 0
also note that x[:n] = 0 does not work if x is a python list (instead of a numpy array).
It is also a bad idea to use [{some object here}] * n for anything mutable, because the list will not contain n different objects but n references to the same object:
>>> a = [[],[],[],[]]
>>> a[0:2] = [["a"]] * 2
>>> a
[['a'], ['a'], [], []]
>>> a[0].append("b")
>>> a
[['a', 'b'], ['a', 'b'], [], []]
0 is the default start, and is therefore redundant.a[0:n] is more symmetric and "easier" to generalize to a[x:y], but it's probably "more pythonic" to use a[:n].Just set them explicitly:
x[0:2] = 0
np_reshaped.transpose()[0:100] = 32001 or np_reshaped.T[0:100] = 32001 should switch axes
x[:N] = 0, if it's actually anumpyarray?