简体   繁体   中英

R's order equivalent in python

Any ideas what is the python's equivalent for R's order ?

order(c(10,2,-1, 20), decreasing = F) 
# 3 2 1 4

In numpy there is a function named argsort

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

Note that python list index starting at 0 while starting at 1 in R.

It is numpy.argsort()

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

# array([2, 1, 0, 3])

and if you want to explore the decreasing = T option. You can try,

(-a).argsort()

#array([3, 0, 1, 2])

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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