Kabou Kabou - 17 days ago 8
Python Question

How to check if an array is multidimensional

I am trying to write a code to check if n is the maximum of the array A if it is return True if it isnt return False but I have two conditions if the value is a multidimentional or unidimentional I don't know how to write that:

if [A is unidimentional]:
maximum=A[0]
for i in range(A.shape[0]):
if max(A[i],maximum)==A[i]:
maximum=A[i]
if max(n,maximum)!=n:
return False
return True

else:
maximum=A[0][0]
for i in range(A.shape[0]):
for j in range(A.shape[1]):
if max(A[i][j],maximum)==A[i][j]:
maximum=A[i][j]
if max(n,maximum)!=n:
return False
return True


if someone knows how I can write that condition it would be very helpful Thanks

Answer

Try the following:

import numpy as np

my_array = np.array([[1,2,3],[4,5,6]])
d = len(my_array.shape)
print(d)  # Output: 2

Now, you can test against d, if its value is 2, then your array is 2 dimensions.