获取列表的升序索引 [英] Getting indices of ascending order of list

查看:71
本文介绍了获取列表的升序索引的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我知道这个问题已经问了一百遍了,但是答案似乎总是使用numpy的argsort".但是,要么我误解了大多数人的要求,要么答案不正确.无论如何,我希望获取列表的升序索引.措辞令人困惑,因此,举个例子,给定列表[4, 2, 1, 3],我希望得到一个列表[3, 1, 0, 2].最小的项是1,因此它获得索引0,最大的项是4,因此它获得索引3.在我看来,经常建议使用argsort,但似乎并没有这样做.

I know that this question has been asked a hundred times, but the answer always seems to be "use numpy's argsort". But either I am misinterpreting what most people are asking, or the answers are not correct for the question. Whatever be the case, I wish to get indices of a list's ascending order. The phrasing is confusing, so as an example, given a list [4, 2, 1, 3] I expect to get a list back [3, 1, 0, 2]. The smallest item is 1, so it gets index 0, the largest one is 4 so it gets index 3. It seems to me that argsort is often suggested, but it just doesn't seem to do that.

from numpy import argsort

l = [4, 2, 1, 3]
print(argsort(l))
# [2, 1, 3, 0]
# Expected [3, 1, 0, 2]

很明显argsort在做其他事情,那么它实际上在做什么,并且它与预期的行为有何相似之处,以至于经常(错误地)建议这样做?而且,更重要的是,如何获得所需的输出?

Clearly argsort is doing something else, so what is it actually doing and how is it similar to the expected behaviour so that it is so often (wrongly) suggested? And, more importantly, how can I get the desired output?

推荐答案

argsoft()基本上将您的列表转换为索引的排序列表.

The argsoft() is basically converting your list to a sorted list of indicies.

l = [4, 2, 1, 3]

首先,它获取列表中每个元素的索引,因此新列表变为:

First it gets index of each element in the list so new list becomes:

indexed=[0, 1, 2, 3]

然后根据原始列表中的项目对索引列表进行排序.为4:0 , 2:1 , 1:2 and 3:3其中:表示对应于".

Then it sorts the indexed list according to the items in the original list. As 4:0 , 2:1 , 1:2 and 3:3 where : means "corresponds to".

排序我们得到的原始列表

Sorting the original list we get

l=[1, 2, 3, 4]

并放置旧列表的每个对应索引的值

And placing values of each corresponding index of old list

new=[2,1,3,0]

因此,基本上,它会根据原始列表对列表的索引进行排序.据我了解.对不起,如果我错了.

So basically it sorts the indicies of a list according to the original list. This is as far as i understood. Sorry if i am wrong.

希望有帮助

这篇关于获取列表的升序索引的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆