问题描述
如果使用len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]))
,我将返回3.
If I use len(np.array([[2,3,1,0], [2,3,1,0], [3,2,1,1]]))
, I get back 3.
为什么对于len()
没有参数,关于多维数组中我要哪个轴的长度?这真是令人震惊.有其他选择吗?
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?
推荐答案
等效嵌套列表的len
是什么?
What is the len
of the equivalent nested list?
len([[2,3,1,0], [2,3,1,0], [3,2,1,1]])
使用shape
的更一般概念,numpy
开发人员选择将__len__
实现为第一个维度. Python将len(obj)
映射到obj.__len__
.
With the more general concept of shape
, numpy
developers choose to implement __len__
as the first dimension. Python maps len(obj)
onto obj.__len__
.
X.shape
返回一个元组,该元组确实具有len
-这是维数X.ndim
. X.shape[i]
选择ith
维(元组索引的直接应用).
X.shape
returns a tuple, which does have a len
- which is the number of dimensions, X.ndim
. X.shape[i]
selects the ith
dimension (a straight forward application of tuple indexing).
这篇关于python中的numpy数组的len()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!