Python报错:IndexError: index 0 is out of bounds for axis 0 with size 0

Python报错:

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

原因:

索引超出了列表的长度。

eg1:

import numpy as np
a = np.empty(1)
print(a[1])
# IndexError: index 1 is out of bounds for axis 0 with size 1

eg2:

import numpy as np
a = np.empty(3)
print(a[5])
# IndexError: index 5 is out of bounds for axis 0 with size 3

解决方法:

检查是自己的索引错了, 还是数组长度定义错了。

本文来自网络,不代表协通编程立场,如若转载,请注明出处:https://net2asp.com/24f54a67ac.html