简体   繁体   English

获取列表升序的索引

[英]Getting indices of ascending order of list

I know that this question has been asked a hundred times, but the answer always seems to be "use numpy's argsort".我知道这个问题已经问了一百次了,但答案似乎总是“使用 numpy 的 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] .措辞令人困惑,因此举个例子,给定一个列表[4, 2, 1, 3]我希望得到一个列表[3, 1, 0, 2] The smallest item is 1 , so it gets index 0 , the largest one is 4 so it gets index 3 .最小的项是1 ,因此它的索引为0 ,最大的项是4因此它的索引为3 It seems to me that argsort is often suggested, but it just doesn't seem to do that.在我看来, argsort经常被建议,但它似乎并没有这样做。

from numpy import argsort

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

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?很明显 argsort 正在做其他事情,那么它实际上在做什么,它与预期的行为有何相似之处,以至于经常(错误地)提出建议? And, more importantly, how can I get the desired output?而且,更重要的是,我怎样才能获得所需的输出?

The argsoft() is basically converting your list to a sorted list of indices. argsoft()基本上是将您的列表转换为索引的排序列表。

l = [4, 2, 1, 3]

First it gets index of each element in the list so new list becomes:首先它获取列表中每个元素的索引,因此新列表变为:

indexed=[0, 1, 2, 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".4:0 , 2:1 , 1:2 and 3:3其中 : 表示“对应于”。

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 indices of a list according to the original list.所以基本上它根据原始列表对列表的索引进行排序。 This is as far as i understood.据我所知,这是。 Sorry if i am wrong.对不起,如果我错了。

The reason why you are not getting the 'right,' or expected, answer is because you are asking the wrong question!您没有得到“正确”或预期的答案的原因是因为您问错了问题!

What you are after is the element rank after sort while Numpy's argsort() returns the sorted index list, as documented!.您所追求的是排序后的元素排名,而 Numpy 的 argsort() 返回已排序的索引列表,如文档所示!。 These are not the same thing (as you found out ;) )!这些不是一回事(正如您所发现的;))!

@hpaulj answered me correctly, but in a comment. @hpaulj 正确回答了我,但在评论中。 And you can't see him.而且你看不见他。 His answer helped me a lot, it allows me to get what I want.他的回答对我帮助很大,它让我得到了我想要的。

import numpy as np
l = [4, 2, 1, 3]
print(np.argsort(np.argsort(l)))

Return:返回:

[3, 1, 0, 2]

This is what you expect.这就是你所期望的。 This method returns the indices for the array if it were sorted.如果数组已排序,则此方法返回数组的索引。

⚠️ But note that if the input array contains repetitions, then there is an interesting effect: ⚠️ 但请注意,如果输入数组包含重复,则有一个有趣的效果:

import numpy as np
l = [4, 2, 1, 3, 4]
print(np.argsort(np.argsort(l)))

Return:返回:

[3 1 0 2 4]

He may not harm you, but he does harm to me.他可能不会伤害你,但他确实伤害了我。 I solve this problem like this:我这样解决这个问题:

import numpy as np

l = [4, 2, 1, 3, 4]

ret2 = np.vectorize(lambda val: np.searchsorted(np.unique(l), val))(l)

print('Returned', ret2)
print('Expected', [3, 1, 0, 2, 3])

Return:返回:

Returned [3 1 0 2 3]
Expected [3, 1, 0, 2, 3]

True, my solution will be slow due to the vectorize function.确实,由于vectorize功能,我的解决方案会很慢。 But nothing prevents you from using numba .但是没有什么可以阻止您使用numba I haven't tested it though 😉.不过我还没有测试过😉。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM