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?


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__



It returns a tuple, which has a len

- number of measurements X.ndim

. X.shape[i]

selects the size ith

(direct tuple indexing application).



Easy. Use .shape


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




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)




All Articles