Am I making some simple mistake or is this a bug? Trying to assign the first and second element of a pair to separate vectors in Python Numpy, but the second element gets assigned to both vectors in all cases.
Desired output: sol[0]
identical to pxvec[i]
.
srange = np.linspace(0, 0.7, 10)
pxvec = srange
pyvec = srange
pindex = 0
for i in srange:
sol=[i,i+1]
print(sol[0],sol[1])
pxvec[pindex]=sol[0]
pyvec[pindex]=sol[1]
print(pxvec[pindex],pyvec[pindex])
pindex=pindex+1
Output I am getting:
0.0 1.0
1.0 1.0
0.07777777777777778 1.0777777777777777
1.0777777777777777 1.0777777777777777
0.15555555555555556 1.1555555555555554
1.1555555555555554 1.1555555555555554
0.23333333333333334 1.2333333333333334
1.2333333333333334 1.2333333333333334
0.3111111111111111 1.3111111111111111
1.3111111111111111 1.3111111111111111
0.3888888888888889 1.3888888888888888
1.3888888888888888 1.3888888888888888
0.4666666666666667 1.4666666666666668
1.4666666666666668 1.4666666666666668
0.5444444444444445 1.5444444444444445
1.5444444444444445 1.5444444444444445
0.6222222222222222 1.6222222222222222
1.6222222222222222 1.6222222222222222
0.7 1.7
1.7 1.7
You need to assign pxvec
and pyvec
to copies of srange
, otherwise every change you will make to pxvec
will also be made to pyvec
and srange
(it's called copy by reference if you want to look this up)
srange = np.linspace(0, 0.7, 10)
pxvec = srange.copy()
pyvec = srange.copy()
And then you can continue with the code you have provided