list indices must be integers or slices, not list
时间: 2023-10-12 12:18:39 浏览: 57
This error occurs when you try to use a list as an index or slice for another list or sequence object, which is not supported in Python.
For example, if you have a list `my_list` and you try to access its elements using another list `index_list`, like this:
```
my_list = [1, 2, 3, 4, 5]
index_list = [1, 2, 3]
result = my_list[index_list]
```
You will get the "list indices must be integers or slices, not list" error because `index_list` is a list and not a valid index or slice for `my_list`.
To fix this error, you need to use integer or slice indices when accessing list elements. For example:
```
my_list = [1, 2, 3, 4, 5]
index_list = [1, 2, 3]
result = [my_list[i] for i in index_list]
```
This will create a new list `result` containing the elements of `my_list` at the indices specified in `index_list`.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![](https://csdnimg.cn/download_wenku/file_type_ask_c1.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)