Creating sort index
时间: 2024-01-21 12:02:05 浏览: 20
To create a sort index in Python, you can use the `sorted()` function. The `sorted()` function takes an iterable object (e.g. list, tuple, dictionary) and returns a new sorted list.
Here's an example:
```python
# create a list of numbers
numbers = [5, 2, 9, 1, 6]
# create a sorted index of the list
sort_index = sorted(range(len(numbers)), key=lambda i: numbers[i])
# print the sort index
print(sort_index)
```
Output:
```
[3, 1, 0, 4, 2]
```
In this example, we first create a list of numbers. Then, we use the `sorted()` function to create a new sorted list of the numbers. We also use the `range()` function to create a range of indices for the original list. Finally, we use a lambda function to create a key function that returns the value of each item in the original list using its index. This key function is used by the `sorted()` function to sort the indices based on the values in the original list. The resulting `sort_index` list contains the indices of the original list in sorted order.