Skip to main content
 首页 » 编程设计

python之n维数组python

2025年05月04日10zlslch

在 python 中,一个 0 填充的二维数组可以使用:

[[0 for x in range(w)] for y in range(h)] 
//where w is the width and h is the height 

我想知道,如何才能做到这一点,以便您可以创建一个函数,该函数可以返回一个用 0 填充的 N 维数组,其中 x、y、z、w 等在数组中指定为参数。

例子:

makeNArray(3, [4, 5, 6]) 
//would make a 3D array which was 4x5x6 

请您参考如下方法:

经过一些编辑我发现我可以这样做:

def nDimensionalArray(n, s): 
    x = 0 
    for i in range(n): 
        x = [x for j in range(s[i])] 
    return x