NumPy数组遍历错误:索引错误:索引1超出了轴0的大小1

2024-03-29 06:54:13 发布

您现在位置:Python中文网/ 问答频道 /正文

带形状的numpy(5,1) 包含以下元素:

[[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

Tags: innumpy元素forinputindex错误range
2条回答

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

N, D = input_array.shape

继续你的代码,或者直接

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

根据错误,N值是错误的。它应该是[0]。你知道吗

使用numpy.ndarray.shape函数计算时应返回正确的行和列值。你知道吗

相关问题 更多 >