1

First of all, I think this is not a variable address copying issue. I have this code :

x = [1, 1, 0, 0]
b_u = [[0.2 for _ in range(3)]] * 4
for i in range(4):
    # The problem is on line below. This line copying new value to the whole column.
    b_u[i][0] = (2 * x[i]) / (1 + sum(x))
print(b_u)

And you can see whats happening in lines below :

# b_u :
# Before loop : [[0.2, 0.2, 0.2], [0.2, 0.2, 0.2], [0.2, 0.2, 0.2], [0.2, 0.2, 0.2]]
# Expected : [[0.67, 0.2, 0.2], [0.67, 0.2, 0.2], [0, 0.2, 0.2], [0, 0.2, 0.2]]
# After loop : [[0.0, 0.2, 0.2], [0.0, 0.2, 0.2], [0.0, 0.2, 0.2], [0.0, 0.2, 0.2]]

Where am I mistaken? Is it a typo?

Soroosh Noorzad
  • 503
  • 1
  • 6
  • 18

0 Answers0