IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices
时间: 2023-11-21 14:06:10 浏览: 220
TypeError: only integer tensors of a single element can be converted to an index
This error message is raised when you try to use invalid indices to access elements of a list or array in Python. The valid indices include integers, slices, ellipsis, numpy.newaxis, and boolean or integer arrays.
For example, if you have a list "my_list" and try to access an element using a string as an index:
```
my_list = [1, 2, 3]
print(my_list['a'])
```
You will get the "IndexError: only integers, slices (`:`), ellipsis (`...`), numpy.newaxis (`None`) and integer or boolean arrays are valid indices" error message because 'a' is not a valid index.
Similarly, if you have a numpy array "my_array" and try to access an element using a float as an index:
```
import numpy as np
my_array = np.array([1, 2, 3])
print(my_array[1.5])
```
You will get the same error message because 1.5 is not a valid index.
阅读全文