0

I have this two versions of a program that generates a magic square of odd order,

1. using numpy arrays :

import numpy as np

N = 5
magic_square = np.zeros((N, N), dtype=int)

n = 1
i, j = 0, N // 2

while n <= N ** 2:
    magic_square[i, j] = n
    n += 1
    newi, newj = (i - 1) % N, (j + 1) % N
    if magic_square[newi, newj]:
        i += 1
    else:
        i, j = newi, newj

print(magic_square)

2. using lists

N = 5
magic_square = [[0] * N] * N

n = 1
i, j = 0, N // 2

while n <= N ** 2:
    magic_square[i][j] = n
    n += 1
    newi, newj = (i - 1) % N, (j + 1) % N
    if magic_square[newi][newj]:
        i += 1
    else:
        i, j = newi, newj

print(magic_square)

But the one which uses lists gives an IndexError: list index out of range for line , magic_square[i][j] = n

Is there any difference in indexing for np arrays and lists ?

If yes, then how do I correct the code containing lists ?

PATHIK GHUGARE
  • 137
  • 1
  • 9

1 Answers1

2

As mentioned by hpaulj, don't use [[0]*N]*N]

>>> l = [[0]*5]*5

[[0, 0, 0, 0, 0], 
 [0, 0, 0, 0, 0], # same as above list
 [0, 0, 0, 0, 0], # same as above list
 [0, 0, 0, 0, 0], # same as above list
 [0, 0, 0, 0, 0]] # same as above list

>>> l[0][0] = 5
[[5, 0, 0, 0, 0],
 [5, 0, 0, 0, 0],
 [5, 0, 0, 0, 0],
 [5, 0, 0, 0, 0],
 [5, 0, 0, 0, 0]]

Use

magic_square = [[0 for i in range(N)] for i in range(N)]
Epsi95
  • 8,832
  • 1
  • 16
  • 34