With numpy arrays, I want to perform this operation:
x[1],...,x[n-1] to x[0],...,x[n-2] (left shift),After some experiments, it is clear that:
nparray (numpy arrays) is a slicing and copying.So the solution is: x[:-1] = x[1:]; x[-1] = newvalue.
Here is a small benchmark:
>>> x = np.random.randint(0, 1e6, 10**8); newvalue = -100
>>> %timeit x[:-1] = x[1:]; x[-1] = newvalue
1000 loops, best of 3: 73.6 ms per loop
>>> %timeit np.concatenate((x[1:], np.array(newvalue).reshape(1,)), axis=0)
1 loop, best of 3: 339 ms per loop
But if you don't need to have a fast access to all values in the array, but only the first or last ones, using a deque is smarter.