Number of rows and columns of a python matrix

Asked

Viewed 3,175 times

-5

How do I know the number of rows and columns of an array in the Python language?

1 answer

3


depends on what you mean by "matrix": The pure python language itself does not have a matrix-specific structure. You can use lists inside lists, but there is no way to determine the size without iterating and counting elements from each list.

A very common alternative to matrix manipulation is the library numpy. It is not part of python and needs to be installed separately, but greatly facilitates the use of matrices and their operations. If you are using numpy, you can use the attribute shape:

import numpy
m = numpy.array([[1, 2, 3], [10, 15, 20]])
print(m.shape)

This will print the dimensions of the numpy matrix (called array):

(2, 3)

Browser other questions tagged

You are not signed in. Login or sign up in order to post.