How to find the size of a tuple?

I have a tuple that looks like

  array=(1,2,3,4)
  lenM = numpy.shape(array)
  print lenM
  (4,)

  if not lenM[1]:
       "Code"

Now, how can I automate my code to find if a tuple is one-dimensional or two-dimensional?

+4
source share
2 answers

You can use numpy.ndimfor this:

In [4]: np.ndim((1,2,3,4))
Out[4]: 1
In [5]: np.ndim(((1,2),(3,4)))
Out[5]: 2
+5
source
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 are multiple dimensions in the array. If len (lenM) is 1, there is only one dimension. if the array has more than one dimension, lenM will have more than one element.

+5
source

Source: https://habr.com/ru/post/1618820/


All Articles