sorted 'int' object is not subscriptable
时间: 2023-07-17 14:57:59 浏览: 143
python sorted函数原理解析及练习
5星 · 资源好评率100%
The error message "sorted 'int' object is not subscriptable" occurs when you try to access a subscript (e.g., index or slice) on an integer object. However, the `sorted()` function in Python expects an iterable object, not a single integer value.
To sort a list of integers, you can use the `sorted()` function with a list as the argument. Here's an example:
```python
numbers = [3, 1, 2]
sorted_numbers = sorted(numbers)
print(sorted_numbers) # Output: [1, 2, 3]
```
Make sure you pass an iterable object (like a list, tuple, or set) to the `sorted()` function instead of a single integer.
阅读全文