等价于基本python中的Numpy.argsort()? [英] Equivalent of Numpy.argsort() in basic python?

查看:60
本文介绍了等价于基本python中的Numpy.argsort()?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

是否有Python的内置函数在python.array上执行 argsort() 是否在numpy.array上执行?

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

推荐答案

我在上面建议了时间,这是我的结果.

I timed the suggestions above and here are my results.

首先,功能:

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__)

现在,IPython会话:

Now, the IPython session:

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

这篇关于等价于基本python中的Numpy.argsort()?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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