How to find dimension of a nested tuple? How to find dimension of a nested tuple? numpy numpy

How to find dimension of a nested tuple?


You can use numpy.ndim for this:

In [4]: np.ndim((1,2,3,4))Out[4]: 1In [5]: np.ndim(((1,2),(3,4)))Out[5]: 2


array=(1,2,3,4)lenM = numpy.shape(array)print lenM(4,)if len(lenM) == 1:    "1 dimensional code"elif len(lenM) == 2:    "2 dimensional code"

len(lenM) will tell you if there is more than one dimension in array. If len(lenM) is 1, there's only one dimension. if array has more than one dimension, lenM will have more than one element.