基本python中Numpy.argsort()的等价物?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3382352/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-18 10:45:24  来源:igfitidea点击:

Equivalent of Numpy.argsort() in basic python?

pythonnumpy

提问by Mermoz

is there a builtin function of Python that does on python.arraywhat argsort()does on a numpy.array?

在那里,做关于Python的内置函数python.array什么argsort()上呢numpy.array

采纳答案by Boris Gorelik

I timed the suggestions above and here are my results.

我对上面的建议进行了计时,这是我的结果。

First of all, the functions:

首先是功能:

def f(seq):
    # http://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python/3383106#3383106
    #non-lambda version by Tony Veijalainen
    return [i for (v, i) in sorted((v, i) for (i, v) in enumerate(seq))]

def g(seq):
    # http://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python/3383106#3383106
    #lambda version by Tony Veijalainen
    return [x for x,y in sorted(enumerate(seq), key = lambda x: x[1])]


def h(seq):
    #http://stackoverflow.com/questions/3382352/equivalent-of-numpy-argsort-in-basic-python/3382369#3382369
    #by unutbu
    return sorted(range(len(seq)), key=seq.__getitem__)

Now, the IPython session:

现在,IPython 会话:

In [16]: seq = rand(10000).tolist()

In [17]: %timeit f(seq)
100 loops, best of 3: 10.5 ms per loop

In [18]: %timeit g(seq)
100 loops, best of 3: 8.83 ms per loop

In [19]: %timeit h(seq)
100 loops, best of 3: 6.44 ms per loop

FWIW

FWIW

回答by unutbu

There is no built-in function, but it's easy to assemble one out of the terrific tools Python makes available:

没有内置函数,但很容易从 Python 提供的极好的工具中组装一个:

def argsort(seq):
    # http://stackoverflow.com/questions/3071415/efficient-method-to-calculate-the-rank-vector-of-a-list-in-python
    return sorted(range(len(seq)), key=seq.__getitem__)

x = [5,2,1,10]

print(argsort(x))
# [2, 1, 0, 3]

It works on Python array.arrays the same way:

array.array以相同的方式在 Python 上工作:

import array
x = array.array('d', [5, 2, 1, 10])
print(argsort(x))
# [2, 1, 0, 3]

回答by Tony Veijalainen

My alternative with enumerate:

我的枚举替代方案:

def argsort(seq):
    return [x for x,y in sorted(enumerate(seq), key = lambda x: x[1])]

seq=[5,2,1,10]
print(argsort(seq))
# Output:
# [2, 1, 0, 3]

Better though to use answer from https://stackoverflow.com/users/9990/marcelo-cantosanswer to thread python sort without lambda expressions

最好使用https://stackoverflow.com/users/9990/marcelo-cantos 的答案来回答没有 lambda 表达式的线程python 排序

[i for (v, i) in sorted((v, i) for (i, v) in enumerate(seq))]

回答by Jeff M.

Found this question, but needed argsort for a list of objects based on an object property.

找到了这个问题,但需要 argsort 来获取基于对象属性的对象列表。

Extending unutbu's answer, this would be:

扩展 unutbu 的答案,这将是:

sorted(range(len(seq)), key = lambda x: seq[x].sort_property)