2

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
6
  • Have you tried anything? Commented Feb 17, 2016 at 10:09
  • I have tried several things involving foor loops and while loops but to no avail Commented Feb 17, 2016 at 10:12
  • You should include some of that in your post along with the results. Commented Feb 17, 2016 at 10:12
  • 2
    Give numpy a look. arr = numpy.zeros((4, 5, 6)). Commented Feb 17, 2016 at 10:14
  • 2
    You should consider using Numpy arrays instead of lists. Commented Feb 17, 2016 at 10:14

1 Answer 1

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

Have you tried changing anything in the array returned from this? You'll see that it doesn't quite behave as expected...

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.