I want to initialize an array that has X two-dimensional elements. For example, if X = 3, I want it to be [[0,0], [0,0], [0,0]]. I know that [0]*3 gives [0, 0, 0], but how do I do this for two-dimensional elements?
I want to initialize an array that has X two-dimensional elements. For example, if
Share
Try this:
In the above code, think of the
3as the number of rows in a matrix, and the2as the number of columns. The0is the value you want to use for initializing the elements. Bear in mind this: if you’re using Python 3, userangeinstead ofxrange.For a more general solution, use this function:
For the particular case of the question:
Alternatively, and if you don’t mind using
numpy, you can use thenumpy.zeros()function as shown in Mellkor’s answer.