How to Know the Dimensions of an Array in Python
In the programming language Python every data point has a particular type such as a string, a float or a list. The type determines what operations you can perform on that data. An array in Python contains multiple values within brackets such as "[ 'dog', 'cat', 'rabbit' ]." An array can contain other arrays such as "[ 'dog', [ 'tabby', 'striped' ], 'rabbit' ]." The number of arrays contained within an array determines its dimensions.
Instructions
-
-
1
Launch Terminal if you are running Mac OS X or launch Command Prompt if you are running Windows.
-
2
Type "python" to launch the Python interpreter. You must have Python installed on your computer for this to work.
-
-
3
Type the following to load some of Python's math functions:
import numpy
-
4
Type the following code to create a multidimensional array:
mda = numpy.zeros([2,3],float)
Change "mda" to any variable name of your choosing.
This prints the following:
array([[ 0., 0., 0.],
[ 0., 0., 0.]]) -
5
Type the following to get the dimensions of the array:
mda.shape
Replace "mda" with the variable name from the last step if you changed it.
The first number you get back is the number of rows followed by the number of columns.
-
1