Ordene una lista en python y luego devuelva el índice de elementos en orden ordenado.
Ejemplos:
Input : [2, 3, 1, 4, 5] Output : [2, 0, 1, 3, 4] After sorting list becomes [1, 2, 3, 4, 5] and their index as [2, 0, 1, 3, 4] Input : [6, 4, 7, 8, 1] Output : [4, 1, 0, 2, 3] After sorting the list becomes [1, 4, 6, 7, 8] and their index as [4, 1, 0, 2, 3].
Método 1
import numpy s = numpy.array([2, 3, 1, 4, 5]) sort_index = numpy.argsort(s) print(sort_index)
Producción :
[2, 0, 1, 3, 4]
Método 2
s = [2, 3, 1, 4, 5] li=[] for i in range(len(s)): li.append([s[i],i]) li.sort() sort_index = [] for x in li: sort_index.append(x[1]) print(sort_index)
Producción:
[2, 0, 1, 3, 4]