繁体   English   中英

numpy数组遍历错误:IndexError:索引1超出了大小为1的轴0的范围

[英]numpy array traversal Error: IndexError: index 1 is out of bounds for axis 0 with size 1

形状为(5,1)且具有以下元素的numpy:

[[1]
 [2]
 [3]
 [4]
 [5]]

您如何遍历并打印每个元素? 如下:

1
2
3
4
5

尝试

for row in range(N):
    for col in range(D):
        print(input_array[row][col])

错误

Error: IndexError: index 1 is out of bounds for axis 0 with size 1

您的N, D值一定是错误的。 要么做

N, D = input_array.shape

并继续您的代码,或直接

for row in input_array:
    for token in row:
        print(token)

根据该错误, N值是错误的。 应该是[0]

使用numpy.ndarray.shape函数进行计算应返回正确的行和列值。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM