def magic(n):
square = []
for i in range(n):
square.append([0] * n)
i = 0
j = (n + 1) // 2
for key in range(1, n ** 2 + 1):
if key % n == 1:
i += 1
else:
i -= 1
j += 1
if i == 0:
i = n
if j > n:
j = 1
square[i - 1][j - 1] = key
return square
matrix = magic(5)
print(matrix)
我這一段程式碼,輸出之後的結果是[[17, 24, 1, 8, 15], [23, 5, 7, 14, 16], [4, 6, 13, 20, 22], [10, 12, 19, 21, 3], [11, 18, 25, 2, 9]]
我想把輸出結果轉成下面這樣要怎麼做?
[[17, 24, 1, 8, 15],
[23, 5, 7, 14, 16],
[4, 6, 13, 20, 22],
[10, 12, 19, 21, 3],
[11, 18, 25, 2, 9]]