-1

I'm sorry if this seems like a silly question, but I swear I searched a lot and I wasn't able to understand any of the responses I found, because they reffered to list of lists and list comprehension, and I think that doesn't apply here (if it does, I'm sorry).

The code is the following:

resultado_anterior = [0, 0, 0, 0]

for k in range(max_iteracao):
    print('variable "resultado_anterior" = ', resultado_anterior[0])
    resultado[0] = matriz[0][4] - (mult(0,1) + mult(0,2) + mult(0,3))
    resultado[1] = matriz[1][4] - (mult(1,0) + mult(1,2) + mult(1,3))
    resultado[2] = matriz[2][4] - (mult(2,0) + mult(2,1) + mult(2,3)) 
    resultado[3] = matriz[3][4] - (mult(3,0) + mult(3,1) + mult(3,2))
    print('variable "resultado_anterior" = ', resultado_anterior[0])
    print('variable "resultado" = ', resultado[0])
    
    resultado_anterior = resultado

My intention is to store the former result of "resultado" in a variable ("resultado_anterior") so that I can compare them and find an error. The strange thing is that, althought the attribution to the variable "resultado_anterior" remains on the last line, its value updates when "resultado" is updated on lines 5 to 8. Here I show the result of the "print" lines. As it's easy to notice above, there's one print before changing the value of the variable "resultado", and another one after it, but still before the change in "resultado_anterior".

variable "resultado_anterior" = 0

variable "resultado_anterior" = 0

variable "resultado" = 9.0

Considering the results above, there's no problem with the code. But the result below is the strangest:

variable "resultado_anterior" = 9.0

variable "resultado_anterior" = 6.458333333333333

variable "resultado" = 6.458333333333333

As you can see, before changing "resultado", the variable "resultado_anterior" equals to 9.0, but its value changes to 6.4583 after updating "resultado", even though it's before the line responsible for the attribution. That way, "resultado" equals to "resultado_anterior" and the error is "equal to zero" when it actually isn't.

In a nutshell, the problem is that the value of the variable "resultado_anterior" is assigned before the line that is responsible for that.

Is there any way to solve that? What surprises me the most is that the error does not occur during the first loop.

1 Answers1

0
resultado_anterior = resultado

Almost certain that the issue is with this line. See this link: List changes unexpectedly after assignment. Why is this and how to prevent it?

You are basically pointing to the same underlying memory object, use list.copy() to get the desired result.

Shffl
  • 396
  • 3
  • 18
  • `resultado.copy()` or `resultado[:]` or `list(resultado)` or `copy.copy(resultado)`. All of them work, there are different circumstances which might favor one or the other (thankfully, with the death of Python 2, you can usually expect `resultado.copy()` to be supported; when you might have to support Python 2, it wasn't an option). – ShadowRanger May 07 '21 at 01:32