# 实现一个argsort

argsort?对数据进行排序，返回的不是排序后数据，而是排序数据的索引

python，借助enumerate对数据的遍历，得到元素的初始index

```python
X=[2,3,4,1,3,3,7]
>>> enumerate(X)
<enumerate object at 0x1069cd168>
>>> list(enumerate(X))
[(0, 2), (1, 3), (2, 4), (3, 1), (4, 3), (5, 3), (6, 7)]
>>> d=dict(enumerate(X))
{0: 2, 1: 3, 2: 4, 3: 1, 4: 3, 5: 3, 6: 7}
>>> d.items()
dict_items([(0, 2), (1, 3), (2, 4), (3, 1), (4, 3), (5, 3), (6, 7)])

# 按值排序items
>>> sorted(d.items(), key=lambda x: x[1])
[(3, 1), (0, 2), (1, 3), (4, 3), (5, 3), (2, 4), (6, 7)]
# 得到的可以就是排序索引
>>> list(r.keys())
[3, 0, 1, 4, 5, 2, 6]
```

```python
# argsort 实现
def argsort(X):
    d = dict(enumerate(X))
    r = dict(sorted(d.items(), key=lambda x: x[1]))
    return list(r.keys())


li = [2,3,4,1,3,3,7]
indies = argsort(li)
print(indies)

# [3, 0, 1, 4, 5, 2, 6]
```


---

# Agent Instructions: Querying This Documentation

If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://im-qianuxn.gitbook.io/pytorch/ji-suan-ji/shua-ti/pai-xu-lei/argsort.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
