0

This is the code I have an issue with:

R = np.ones(16) * -100
for i in np.arange(10):
    print('Range of 100: ', i , ' Repetitions finished')
    R_save = R
    print(R_save)
    R[0] = 3
    print(R)
    print(R_save)
    comparison = R == R_save
    if comparison.all():
        print('finished Range of 100 potences')
        print(R)
        print(R_save)
        break

The idea is to change the first entry of the R-array in the first repetition while keeping the R_save-array the same. In the second repetition the if condition should be TRUE and stop the loop. However the R_save entry changes along with the R entry. If someone could help me understand how to get around this issue or better understand it I would much appreciate it :) Thank you so much! Alex

Barmar
  • 741,623
  • 53
  • 500
  • 612
Alex
  • 1

1 Answers1

1

R and R_save are two variables pointing to the same array, so you cannot "change only one" - there is only one array, with two variables referencing it.

Instead, you can copy R when you create R_save, so there really are two different arrays with (initially) the same elements:

R_save = np.copy(R)
Mureinik
  • 297,002
  • 52
  • 306
  • 350