Len () from numpy array in python

If I use len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]))

I will return 3.

Why are there no arguments for len()

about which axis do I need the length in multidimensional arrays? This is alarming. Is there any alternative?

+3
python numpy multidimensional-array variable-length


source to share


3 answers


What is an len

equivalent nested list?

len([[2,3,1,0], [2,3,1,0], [3,2,1,1]])

      



With a more general concept shape

, the numpy

developers decided to implement __len__

as the first dimension. Python maps len(obj)

to obj.__len__

.

X.shape

It returns a tuple, which has a len

- number of measurements X.ndim

. X.shape[i]

selects the size ith

(direct tuple indexing application).

+9


source to share


Easy. Use .shape

.



>>> nparray.shape
(5, 6) #Returns a tuple of array dimensions.

      

+4


source to share


You can move the array if you want to get the length of another dimension.

len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]).T)

      

0


source to share







All Articles
Loading...
X
Show
Funny
Dev
Pics