【发布时间】:2017-01-30 08:54:13
【问题描述】:
我知道 numpy 数组有一个名为 shape 的方法,它返回 [No.of rows, No.of columns],shape[0] 给出行数,shape[1] 给出列数。
a = numpy.array([[1,2,3,4], [2,3,4,5]])
a.shape
>> [2,4]
a.shape[0]
>> 2
a.shape[1]
>> 4
但是,如果我的数组只有一行,那么它会返回 [No.of columns, ]。并且 shape[1] 将超出索引。例如
a = numpy.array([1,2,3,4])
a.shape
>> [4,]
a.shape[0]
>> 4 //this is the number of column
a.shape[1]
>> Error out of index
如果数组可能只有一行,我如何获取 numpy 数组的行数?
谢谢
【问题讨论】: