等价于基本python中的Numpy.argsort()?[重复]
问题内容:
这个问题已经在这里有了答案 :
如何在Python中获取排序数组的索引 (11个答案)
5年前关闭。
在那里,做关于Python的内置函数python.array
什么argsort()
上呢numpy.array
?
问题答案:
我在上面建议了时间,这是我的结果。
首先,功能:
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会话:
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
第一次世界大战