Python how to quickly initialize a two-dimensional array

The correct way
cols =. 3
rows = 2

memo = [[1] * m for i in range(n) ]

Below this approach is wrong


memo = [[1] * m ] * n

This causes the memo [0] == memo [1], wherein changing the value of a sub-array, the array will change along with the other sub-

Guess you like

Origin www.cnblogs.com/yeni/p/11590108.html