Python multidimensional array initialization (multiple methods)

Usually, whether it’s a brush or actual project, you will encounter the problem of multi-dimensional arrays. Several methods of initializing multi-dimensional arrays are summarized. The following
method is recorded :
two-dimensional array
For example: dp[n][2] The initialization is
more in front Dimensions, generally appear more behind

dp=[[0 for j in range(2)]for i in range(n)]

Method Two:

dp=[[0,0] for _ in range(n)]

Method three:
Use numpy
numpy.empty with numpy.fill use
note directly empty is random assignment of

Two-dimensional matrix

Three-dimensional matrix
Initialize shape(3,4,5) The
shape is divided into 3 layers, each layer has 4 rows and 5 columns

Method 4:
Use numpy zeros

Guess you like

Origin blog.csdn.net/weixin_39666736/article/details/104206298