If I use len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]))
, I get back 3.
Why is there no argument for len()
about which axis I want the length of in multidimensional arrays? This is alarming. Is there an alternative?
If I use len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]))
, I get back 3.
Why is there no argument for len()
about which axis I want the length of in multidimensional arrays? This is alarming. Is there an alternative?
Easy. Use
.shape
.What is the
len
of the equivalent nested list?With the more general concept of
shape
,numpy
developers choose to implement__len__
as the first dimension. Python mapslen(obj)
ontoobj.__len__
.X.shape
returns a tuple, which does have alen
- which is the number of dimensions,X.ndim
.X.shape[i]
selects theith
dimension (a straight forward application of tuple indexing).You can transpose the array if you want to get the length of the other dimension.