Python.Runtime.PythonException:“TypeError : list indices must be integers or slices, not tuple”
时间: 2023-10-14 20:19:40 浏览: 107
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
5星 · 资源好评率100%
This error occurs when you try to use a tuple as an index for a list in Python. Lists can only be indexed using integers or slices, not tuples.
To fix this error, you need to make sure that you are using integers or slices as the index for your list. If you need to use a tuple as an index, you can convert it to an integer or slice before using it.
For example, if you have a list of lists and you want to access a specific element using a tuple as the index, you can convert the tuple to integers like this:
```
my_list = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
my_tuple = (1, 2)
my_int_index = my_tuple[0] * len(my_list[0]) + my_tuple[1]
print(my_list[my_int_index])
```
This will print the element at index (1, 2) in the list, which is 6.
阅读全文