简体   繁体   English

在python numpy中指定2d数组的大小

[英]specifying size of a 2d array in python numpy

I would like to statically initialize the size of an N by N 2d array in python's numpy where N is a variable (from a SQL query). 我想在python的numpy中静态初始化N by N 2d数组的大小,其中N是变量(来自SQL查询)。 The equivalent Java would be: 等效的Java将是:

N = code from sql query
int[][] mat = new int[N][N]

How would I do this in numpy? 我将如何在numpy中执行此操作? Or what about a Matrix type? 或矩阵类型呢?

You can do it in a number of ways, but to create an empty array that you will later fill, you might consider: 您可以通过多种方法来完成此操作,但是要创建一个以后要填充的空数组,可以考虑:

N = 100
mat = np.empty((N,N))

There are a large number of other methods detailed in the docs: 在文档中详细介绍了许多其他方法:

http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html http://docs.scipy.org/doc/numpy/reference/routines.array-creation.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM