在 Python 中初始化空矩阵
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18449136/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Initialize empty matrix in Python
提问by marriam nayyer
I am trying to convert a MATLAB code in Python. I don't know how to initialize empty matrix in Python.
我正在尝试在 Python 中转换 MATLAB 代码。我不知道如何在 Python 中初始化空矩阵。
MATLAB Code:
MATLAB 代码:
demod4(1) = [];
I tried in Python
我在 Python 中尝试过
demod4[0] = array([])
but it gives error:
但它给出了错误:
only length-1 arrays can be converted to Python scalars
采纳答案by gevang
If you are using numpy
arrays, you initialize to 0, by specifying the expected matrix size:
如果您使用的是numpy
arrays,则通过指定预期的矩阵大小来初始化为 0:
import numpy as np
d = np.zeros((2,3))
>>> d
[[ 0. 0. 0.]
[ 0. 0. 0.]]
This would be the equivalent of MATLAB 's:
这相当于 MATLAB 的:
d = zeros(2,3);
You can also initialize an empty array, again using the expected dimensions/size
您还可以初始化一个空数组,再次使用预期的维度/大小
d = np.empty((2,3))
If you are not using numpy, the closest somewhat equivalent to MATLAB's d = []
(i.e., a zero-size matrix) would be using an empty list and then
如果您不使用 numpy,则最接近 MATLAB 的d = []
(即零大小矩阵)将使用一个空列表,然后
append values (for filling a vector)
附加值(用于填充向量)
d = []
d.append(0)
d.append(1)
>>> d
[0, 1]
or append lists (for filling a matrix row or column):
或附加列表(用于填充矩阵行或列):
d = []
d.append(range(0,2))
d.append(range(2,4))
>>> d
[[0, 1], [2, 3]]
See also:
也可以看看:
初始化一个 numpy 数组(SO)
NumPy array initialization (fill with identical values)(SO)
回答by wflynny
What about initializing a list, populating it, then converting to an array.
初始化一个列表,填充它,然后转换为一个数组怎么样。
demod4 = []
Or, you could just populate at initialization using a list comprehension
或者,您可以在初始化时使用列表理解进行填充
demod4 = [[func(i, j) for j in range(M)] for i in range(N)]
Or, you could initialize an array of all zeros if you know the size of the array ahead of time.
或者,如果您提前知道数组的大小,则可以初始化一个全为零的数组。
demod4 = [[0 for j in range(M)] for i in range(N)]
or
或者
demod4 = [[0 for i in range(M)]*N]
Or try using numpy
.
或者尝试使用numpy
.
import numpy as np
N, M = 100, 5000
np.zeros((N, M))
回答by darmat
You could use a nested list comprehension:
您可以使用嵌套列表理解:
# size of matrix n x m
matrix = [ [ 0 for i in range(n) ] for j in range(m) ]
回答by user8154599
M=[]
n=int(input())
m=int(input())
for j in range(n):
l=[]
for k in range(m):
l.append(0)
M.append(l)
print(M)
This is the traditional way of doing it matrix[m,n], However, python offers many cool ways of doing so as mentioned in other answers.
这是执行 matrix[m,n] 的传统方法,但是,如其他答案中所述,python 提供了许多很酷的方法。
回答by yanefedor
To init matrix with M rows and N columns you can use following pattern:
要使用 M 行 N 列初始化矩阵,您可以使用以下模式:
M = 3
N = 2
matrix = [[0] * N for _ in range(M)]
回答by Luis Mario
rows = 3
columns = 2
M = [[0]*columns]*rows
Or you could also use '' instead of 0
或者你也可以使用 '' 而不是 0
print(M)
Output:
输出:
M = [[0, 0], [0, 0], [0, 0]]