I’m trying to implement a function that initializes an arbitrarily sized list with objects, similar to how numpy array initialization methods work.
def fill(shape, object):
I’ve been banging my head against this but can’t think of a way to do this for arbitrary length dimensionality. I’m guessing it will require recursion of some sort.
Here is an example of the desired behavior. For simplicity the object is just the floating-point number 0, but I need this to work with any class:
> fill( (2, 3, 4), 0.)
[
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]
,
[[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.],
[ 0., 0., 0., 0.]]
]
Try the following recursive implementation
A non recursive version