如何在python中定义多维数组?

时间:2022-04-30 21:34:09

In MATLAB there is an easy way to define multidimensional arrays e.g.

在MATLAB中,有一种简单的方法来定义多维数组,例如

A(:,:,1) = [1,2,3; 4,5,6];
A(:,:,2) = [7,8,9; 10,11,12];

>> A

 A(:,:,1) =

 1     2     3
 4     5     6


 A(:,:,2) =

 7     8     9
 10    11    12

where the first two indices are respectively, for the rows and columns of the ith matrix (or page, see picture below) stored in A;

其中前两个索引分别为存储在A中的第i个矩阵(或页面,见下图)的行和列;

如何在python中定义多维数组?

Does anybody know how can I define the same structure in python?

有谁知道如何在python中定义相同的结构?

3 个解决方案

#1


9  

with NumPy indexing is similar to MATLAB

与NumPy索引类似于MATLAB

 import numpy as np
 A=np.empty((2,3,3))
 A.shape
 #(2L, 3L, 3L)
 A[0,1,2] # element at index 0,1,2
 #0.0
 A[0,:,:] # 3x3 slice at index 0
 #array([[ 0.,  0.,  0.],
 #       [ 0.,  0.,  0.],
 #       [ 0.,  0.,  0.]])
 A[1,1,:] # 1-D array of length 3
 #array([ 0.,  0.,  0.]

#2


8  

A pure Python way to do this is using a list of lists (or in this case a list of lists of lists). You can initialize it with list comprehension. For instance:

纯Python方法是使用列表列表(或者在这种情况下是列表列表)。您可以使用列表解析来初始化它。例如:

w = 4 #width
h = 3 #height
d = 3 #depth

data = [[[0]*h for _ in range(w)] for _ in range(d)]

Or if you want to fill the tensor with tuples like on the figure:

或者如果你想用图中的元组填充张量:

data = [[[(i+1,j+1,k+1) for k in range(h)] for j in range(w)] for i in range(d)]

This initializes a d x w x h "matrix" filled with zeros.

这初始化了一个用零填充的d x w x h“矩阵”。

You can then access the (i,j,k)-th element with:

然后,您可以使用以下命令访问(i,j,k)-th元素:

data[i][j][k]

Nevertheless there are libraries like that have support for vectors, matrices, tensors, etc.

然而,像numpy这样的库可以支持向量,矩阵,张量等。

#3


2  

If you're willing to use NumPy then there's plenty of ways. One way would be to initialise with all zeros or, as in your updated example, you could also fill with a range and then reshape.

如果您愿意使用NumPy,那么有很多方法。一种方法是使用全零来初始化,或者如在更新的示例中,您还可以填充范围然后重新整形。

import numpy as np

a = np.arange(48, dtype=np.int64).reshape((3, 4, 4))
# or 
b = np.zeros((3, 4, 4), dtype=np.int64)

#1


9  

with NumPy indexing is similar to MATLAB

与NumPy索引类似于MATLAB

 import numpy as np
 A=np.empty((2,3,3))
 A.shape
 #(2L, 3L, 3L)
 A[0,1,2] # element at index 0,1,2
 #0.0
 A[0,:,:] # 3x3 slice at index 0
 #array([[ 0.,  0.,  0.],
 #       [ 0.,  0.,  0.],
 #       [ 0.,  0.,  0.]])
 A[1,1,:] # 1-D array of length 3
 #array([ 0.,  0.,  0.]

#2


8  

A pure Python way to do this is using a list of lists (or in this case a list of lists of lists). You can initialize it with list comprehension. For instance:

纯Python方法是使用列表列表(或者在这种情况下是列表列表)。您可以使用列表解析来初始化它。例如:

w = 4 #width
h = 3 #height
d = 3 #depth

data = [[[0]*h for _ in range(w)] for _ in range(d)]

Or if you want to fill the tensor with tuples like on the figure:

或者如果你想用图中的元组填充张量:

data = [[[(i+1,j+1,k+1) for k in range(h)] for j in range(w)] for i in range(d)]

This initializes a d x w x h "matrix" filled with zeros.

这初始化了一个用零填充的d x w x h“矩阵”。

You can then access the (i,j,k)-th element with:

然后,您可以使用以下命令访问(i,j,k)-th元素:

data[i][j][k]

Nevertheless there are libraries like that have support for vectors, matrices, tensors, etc.

然而,像numpy这样的库可以支持向量,矩阵,张量等。

#3


2  

If you're willing to use NumPy then there's plenty of ways. One way would be to initialise with all zeros or, as in your updated example, you could also fill with a range and then reshape.

如果您愿意使用NumPy,那么有很多方法。一种方法是使用全零来初始化,或者如在更新的示例中,您还可以填充范围然后重新整形。

import numpy as np

a = np.arange(48, dtype=np.int64).reshape((3, 4, 4))
# or 
b = np.zeros((3, 4, 4), dtype=np.int64)