R的顺序等价于python

Dee*_*ena 8 python r python-2.7

任何想法什么是R的蟒蛇的等价物order

order(c(10,2,-1, 20), decreasing = F) 
# 3 2 1 4
Run Code Online (Sandbox Code Playgroud)

Phi*_*ang 8

在numpy中有一个名为的函数 argsort

import numpy as np
lst = [10,2,-1,20]
np.argsort(lst)
# array([2, 1, 0, 3]) 
Run Code Online (Sandbox Code Playgroud)

请注意,在R中从1开始时,python列表索引从0开始.


Ron*_*hah 6

它是 numpy.argsort()

import numpy
a = numpy.array([10,2,-1, 20])
a.argsort()

# array([2, 1, 0, 3])
Run Code Online (Sandbox Code Playgroud)

以及是否要探索该decreasing = T选项。你可以试试,

(-a).argsort()

#array([3, 0, 1, 2])
Run Code Online (Sandbox Code Playgroud)