I want to create a function that returns a matrix with a given shape, similar to what np.zeros(shape) would do in numpy.
I tried the following, which works:
shape = [1,2,3]
x = [ lambda : 0 ]
x.append( lambda : [ x[0]() for _ in range(shape[0])] )
x.append( lambda : [ x[1]() for _ in range(shape[1])] )
x.append( lambda : [ x[2]() for _ in range(shape[2])] )
print(x[-1]())
But when I try to automize this to work with any amount of dimensions as follows, I get a RecursionError.
shape = [1,2,3]
x = [ lambda : 0 ]
for i,s in enumerate(shape):
x.append( lambda: [ x[i]() for _ in range(s)] )
print(x[-1]())
Does someone know what went wrong here, or if there is a better way to do this?