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

解决方法:
检查是自己的索引错了, 还是数组长度定义错了。

你可能感兴趣的:(Python常见报错,Python,python,numpy,开发语言)