0

Why would the following code assign the same values to each row of the percent errors list when a nested for loop iterating over sizes [1*10,columns*10] j times produces unique outputs?

import numpy as np

columns = 2
rows = 3
percenterrors = [[0]*columns]*rows

for i in range(1, rows+1):
  for j in range(1,columns+1):
    sample = np.random.poisson(lam = 27, size = (10*j))
    results[i-1][j-1] = sample
    #sns.distplot(sample)
    percenterrors[i-1][j-1] = (np.var(sample)-27)/27
Nick ODell
  • 15,465
  • 3
  • 32
  • 66
user3163829
  • 221
  • 1
  • 2
  • 7
  • 2
    `[[0]*columns]*rows` This does not create many different lists. This creates a list with many copies of the same list. Changing one element will change many elements. – Nick ODell Mar 22 '23 at 21:35

0 Answers0