current position:Home>[Python] create a two-dimensional array with list, which is easy to step on

[Python] create a two-dimensional array with list, which is easy to step on

2022-05-15 05:57:30__ Watson__

list Create a one-dimensional array ,

arr = [0] * n

That's all right.

list Create a 2D array

arr = [[0] * n] * m

There is a problem , Inside [0] * n Is the same reference , Change one of them , Will modify all .
The correct way is to use list derivation

a = [ [0 for j in range(n)] for i in range(m)]

copyright notice
author[__ Watson__],Please bring the original link to reprint, thank you.
https://en.pythonmana.com/2022/131/202205110609598994.html

Random recommended