创建多维零位Python [英] Create Multidimensional Zeros Python

查看:57
本文介绍了创建多维零位Python的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I need to make a multidimensional array of zeros.

对于两个(D = 2)或三个(D = 3)尺寸,这很容易,我会使用:

For two (D=2) or three (D=3) dimensions, this is easy and I'd use:

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

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

对于高D的I,如何使长度为n的数组?

How for I for higher D, make the array of length n?

推荐答案

您可以将元组(n,)乘以所需的维数.例如:

You can multiply a tuple (n,) by the number of dimensions you want. e.g.:

>>> 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的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆