#๐Ÿ”’ Why my original array value keep changing even I already make a new array for it

18 messages ยท Page 1 of 1 (latest)

open moss
#
fx = np.array([
    [0, 5, 10, 15],
    [5, 0, 5, 10],
    [10, 5, 0, 15],
    [15, 10, 15, 0]
    ])

fxaksen = fx
    
i = 1
iteration = 0
while i <= m * n:  
    j = i + 1
    while j <= m * n:
        print("Iteration:", iteration,"------------------------------")
        print("i:", i, "j:", j)
        
        print()
        print(fx, "fx")
        
        print()
        print(fxaksen, "fxaksen")
        
        irow, jrow = [-1, -1]
        icol, jcol = [-2, -2]
        
        fxaksen[i - 1, :],  fxaksen[j - 1, :] = irow, jrow
        fxaksen[:, i - 1],  fxaksen[:, j - 1] = icol, jcol
        
        print()
        print(fx, "fx after")
        
        print()
        print(fxaksen, "fxaksen after")
        
        j += 1
        iteration += 1
    i += 1

Here is my code, but my original "fx" keep changing following the "fxaksen value"

I don't understand, I loop only for the "fxaksen" value, but the "fx" value also changes

I want my "fx" print in the loop print its original value

zinc perchBOT
#

@open moss

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

white basin
#

You may be dealing with the concept of a "view".

#

Let's see.

wild python
#

fxaksen = fx means fxaksen points to the same value as fx, python does not do value copies when assigning

#

if this were a list, you could say =fx.copy(), but this is np and i'm not so familiar with that

white basin
#

Ah, didn't spot that. Should have. Got distracted by the below.

#

I think of variables as like signposts.

#

They aren't the object, themselves, just a reference to it.

#

So when you do a = b, you are pointing the a signpost at the same object that b is pointing to.

open moss
white basin
#

Where you can treat the variable as the object itself for any purpose other than reassigning the variable to a different object.

open moss
#

it's working, ty all have a nice day โ˜€๏ธ

#

!close

zinc perchBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.