python - 创建多维零点 Python

标签 python arrays multidimensional-array numpy

我需要制作一个由零组成的多维数组。

对于两个 (D=2) 或三个 (D=3) 维度,这很简单,我会使用:

a = numpy.zeros(shape=(n,n)) 

a = numpy.zeros(shape=(n,n,n))

对于更高的 D,如何制作长度为 n 的数组?

最佳答案

您可以将元组 (n,) 乘以您想要的维数。例如:

>>> import numpy as np
>>> N=2
>>> np.zeros((N,)*1)
array([ 0.,  0.])
>>> np.zeros((N,)*2)
array([[ 0.,  0.],
       [ 0.,  0.]])
>>> np.zeros((N,)*3)
array([[[ 0.,  0.],
        [ 0.,  0.]],

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

关于python - 创建多维零点 Python,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15393216/

相关文章:

python - 在 python 存储库名称和包名称中使用连字符/破折号

python - 增加一个类别

python - 如何将某物称为对象而不是列表?

将 char 数组转换为 hex 数组

c - 用零初始化一个二维数组;打印其值表明并非每个元素都包含 0

Javascript 查找两个二维数组之间的共同值

python - 动态组装 Python 模块,动态导入

python - Heroku 无法安装 pywin32 库

.net - 在 C++/CLI 中将 std::vector<>::iterator 转换为 .NET 接口(interface)

c - 将三维数组传递给 C 中的函数?