Create Multidimensional Zeros Python

后端 未结 4 1001
一生所求
一生所求 2020-12-19 04:02

I need to make a multidimensional array of zeros.

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

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


        
4条回答
  •  孤城傲影
    2020-12-19 04:50

    In [4]: import numpy
    
    In [5]: n = 2
    
    In [6]: d = 4
    
    In [7]: a = numpy.zeros(shape=[n]*d)
    
    In [8]: a
    Out[8]: 
    array([[[[ 0.,  0.],
             [ 0.,  0.]],
    
            [[ 0.,  0.],
             [ 0.,  0.]]],
    
    
           [[[ 0.,  0.],
             [ 0.,  0.]],
    
            [[ 0.,  0.],
             [ 0.,  0.]]]])
    

提交回复
热议问题