0

I want to update only 1 element in the 1d array and then start over fresh. if viewed as a matrix form I just want entries i = j to be changed.

my code so far:

import numpy as np
a = np.array([10, 20, 30, 40, 50])

for i, j in enumerate(a):
    b = a
    b[i] = j + 1
    print(b)

I want each iteration of the for loop to only change one element and keep everything else the same.

the output I want looks like this:

[11, 20, 30, 40, 50]
[10, 21, 30, 40, 50]
[10, 20, 31, 40, 50]
[10, 20, 30, 41, 50]
[10, 20, 30, 40, 51]

but I'm getting this because b is not resetting even though I am (or at lest i think) restoring the original array at the start of each loop.

[11, 20, 30, 40, 50]
[11, 21, 30, 40, 50]
[11, 21, 31, 40, 50]
[11, 21, 31, 41, 50]
[11, 21, 31, 41, 51]

any ideas where I went wrong? TIA

1
  • 1
    Replace b=a with b=a.copy() Commented Sep 30, 2018 at 3:09

1 Answer 1

1

Try replacing b=a with b=a.copy() b=a, will create b and point to the same memory. Whereas b=a.copy(), creates a copy of a and stores it as b in different memory location.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.