When assigning a value from an array to another one, the array on the right hand side changes. Could you point me towards the possible mistake that I make?
The code below finds longest increasing sequence and I have the problem with the last line yy[-1] = y[n]. Whenever this line executed a value in y also changes.
import numpy as np
p = np.array([466, 5500, 2615, 4056, 2196, 4254, 2987, 5125, 1060, 7344, 2990])
y = p.argsort()
yy = y[-2:]
yy = yy[::-1]
n = len(y)-2
while(n>0):
n = n-1
if (y[n] < yy[-1]):
yy = np.append(yy,y[n])
if ((y[n] > yy[-1]) & (y[n] < yy[-2])):
yy[-1] = y[n]
yyandyare different views of the same array. Hence all operations on shared positions effect bothyandyynp.appendthere, that does return a new array at some point. BTW, usingnp.appendin a loop is highly inefficient. If you really need fast lightweight dynamic C-arrays with in-placeappend, usearray.array. Just convert it to a NumPy array once the loop is over, if you need special NumPy functionality.