• python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Facebook Twitter Instagram
Devs Fixed
  • python
  • javascript
  • reactjs
  • sql
  • c#
  • java
Devs Fixed
Home ยป Resolved: In Python, why my variable StartingU get updated when I only update the variable AverageU in my for loops? What is wrong with the code?

Resolved: In Python, why my variable StartingU get updated when I only update the variable AverageU in my for loops? What is wrong with the code?

0
By Isaac Tonny on 18/06/2022 Issue
Share
Facebook Twitter LinkedIn

Question:

The purpose is to calculate the average value (AverageU) from a starting array (StartingU)
The for loops that calculate the average values for AverageU
Output:
The problem is why StartingU gets updated? It should be unchanged

Answer:

AverageU changed since this code, not after for loop.
AverageU and StartingU are the same instances. You can check it with is function.
You should make a new instance as the comment said.
AverageU = StartingU This code just makes another reference to the same object with a new name and this indicates the same memory. You can check the memory address of the variable with function id
You can compare like this.
Notice
Copy with the colon is actually a shallow copy, it copies only the reference of the nested list.

If you have better answer, please add a comment about this, thank you!

arrays numpy python variables
Share. Facebook Twitter LinkedIn

Related Posts

Resolved: Java Swing center JLabel in JPanel with two JLabels

27/03/2023

Resolved: How to compare two text files and and change the sign of the data in powershell?

27/03/2023

Resolved: Java Virtual Machines deleted

27/03/2023

Leave A Reply

© 2023 DEVSFIX.COM

Type above and press Enter to search. Press Esc to cancel.