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

1
  • R_save = R.copy() Commented Jan 6, 2023 at 18:20

1 Answer 1

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)
Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.