I want to convert this piece of code in order to make it compatible with Numba. The only sort method that Numba support is sorted() but not with the key arg. I have to manualy sort without other lib imports or maybe just some numpy. Someone could give me an efficient way to do this sort ? Thanks
import random
n = 1000
index = list(range(n))
keys = list(range(n))
random.shuffle(keys)
index.sort(key=lambda x: keys[x])) <= HOW TO CONVERT THIS ?
CodePudding user response:
For this particular kind of input, you can achieve the sorting with:
for value in index[:]:
index[keys[value]] = value
If the keys are not a permutation from a range(n)
(like in your question), then create temporary tuples, call sorted
and then extract the value again from the tuples:
result = [value for _, value in sorted(
[(keys[value], value) for value in index]
)]