list indices must be integers or slices, not str
时间: 2023-10-12 09:16:00 浏览: 38
This error message occurs when you try to use a string as an index for a list in Python.
For example, if you have a list called "my_list" and you try to access an element using a string instead of an integer or slice, you will get this error:
```
my_list = [1, 2, 3, 4, 5]
print(my_list['a'])
```
This will result in the following error message:
```
TypeError: list indices must be integers or slices, not str
```
To fix this error, you need to make sure that you are using an integer or slice as the index for the list. For example:
```
my_list = [1, 2, 3, 4, 5]
print(my_list[0])
```
This will output:
```
1
```
Alternatively, if you need to access an element of a list using a string, you could use a dictionary instead of a list. In a dictionary, you can use strings as keys to access values.
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![py](https://img-home.csdnimg.cn/images/20210720083646.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.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)
![csv](https://img-home.csdnimg.cn/images/20210720083646.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)