TypeError: tuple indices must be integers or slices, not tuple
时间: 2023-09-27 20:08:20 浏览: 46
This error occurs when you try to use a tuple as an index for another tuple or sequence, but the tuple itself contains elements that are not integers or slices.
For example, consider the following code:
```
t = (1, 2, 3)
print(t[(1, 2)])
```
This will result in a TypeError, because the tuple `(1, 2)` is being used as an index for the tuple `t`, and tuples can only be indexed with integers or slices.
To fix this error, you need to use integers or slices as indices. For example:
```
t = (1, 2, 3)
print(t[1]) # prints 2
```
If you need to use a tuple as an index, you can unpack it into integers or slices using the `*` operator. For example:
```
t = (1, 2, 3)
index = (1, 2)
print(t[*index]) # prints 3
```
相关推荐
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![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)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![pdf](https://img-home.csdnimg.cn/images/20210720083512.png)
![rar](https://img-home.csdnimg.cn/images/20210720083606.png)
![zip](https://img-home.csdnimg.cn/images/20210720083736.png)
![txt](https://img-home.csdnimg.cn/images/20210720083642.png)