n维数组python

时间:2016-02-17 10:09:00

标签: python arrays for-loop

在python中,可以使用以下方法实现0填充的2D数组:

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

我想知道,怎么可以这样做,所以你可以创建一个函数,它可以返回一个填充0s的N维数组,其中x,y,z,w等在数组中指定为参数。

示例:

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

1 个答案:

答案 0 :(得分:1)

经过一些编辑,我发现我可以做到:

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