Ahdee Ahdee - 2 months ago 8
Python Question

numpy, how do I find total rows in a 2D array and total column in a 1D array

Hi apologies for the newbie question, but I'm wondering if someone can help me with two questions.
Example say I have this,

[[1,2,3],[10,2,2]]

I have two questions.


  • How do I find total columns:

  • How do I find total rows:



thank you very much.
A

Answer

Getting number of rows and columns is as simple as:

>>> import numpy as np
>>> a=np.array([[1,2,3],[10,2,2]])
>>> num_rows, num_cols = a.shape
>>> print num_rows, num_cols
2 3