python中的numpy数组的len() [英] len() of a numpy array in python

查看:1482
本文介绍了python中的numpy数组的len()的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果使用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()的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆