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])
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With