如果你真的想要一个矩阵,你最好用numpy..矩阵运算numpy通常使用二维数组类型。创建新数组的方法有很多;最有用的方法之一是zeros函数,它接受一个形状参数并返回给定形状的数组,其值初始化为零:>>> import numpy>>> numpy.zeros((5, 5))array([[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.],

[ 0., 0., 0., 0., 0.]])

numpy提供一个matrix也要打字。它不太常用,而且有些人不建议用它。但它对人们来说很有用numpy在其他情况下。我想我应该把它包括进去,因为我们说的是矩阵!>>> numpy.matrix([[1, 2], [3, 4]])matrix([[1, 2],

[3, 4]])

下面是创建二维数组和矩阵的一些其他方法(为压缩而删除输出):

numpy.matrix('1 2; 3 4') # use Matlab-style syntax

numpy.arange(25).reshape((5, 5)) # create a 1-d range and reshape

numpy.array(range(25)).reshape((5, 5)) # pass a Python range and reshape

numpy.array([5] * 25).reshape((5, 5)) # pass a Python list and reshape

numpy.empty((5, 5)) # allocate, but don't initialize

numpy.ones((5, 5)) # initialize with ones

numpy.ndarray((5, 5)) # use the low-level constructor