In python, a 0 filled 2D array can be achieved using:
[[0 for x in range(w)] for y in range(h)]
//where w is the width and h is the height
I was wondering, how could this be done so you could create a function which could return a N dimensional array filled with 0s, where the x,y,z,w etc were specified in an array as a parameter.
Example:
makeNArray(3, [4, 5, 6])
//would make a 3D array which was 4x5x6
asked Feb 17, 2016 at 10:08
1 Answer 1
After some editing I found that I could do:
def nDimensionalArray(n, s):
x = 0
for i in range(n):
x = [x for j in range(s[i])]
return x
answered Feb 17, 2016 at 10:14
Sign up to request clarification or add additional context in comments.
1 Comment
holroy
Have you tried changing anything in the array returned from this? You'll see that it doesn't quite behave as expected...
lang-py
arr = numpy.zeros((4, 5, 6))
.