Rank the contents of a Python list 3

Asked

Viewed 115 times

-1

For example I have a list

[6,4,3,9,1]

the answer to that would be the index of the highest value of this list in example order to the top list

[1,3,2,0,4]

these are the indices of where their indices are, I’ve done so far:

def Cria_listas(tl,tv):
    valor = []
    if tv >= 1:
       for i in range(tl):
          valor = valor + [random.randint(0,tv]
       print(valor)
       return Fromint(valor)

def Fromint(n):
   indice = []
   for i in range(len(n)):
      indice = [n.index(max(n))] + indice

Cria_listas(5,99)

I can find the intel of the highest value, but how do I find the next

  • 1

    Not understood, want to return a new list with the indexes of the ordered elements?

  • Instead of [1,3,2,0,4] shouldn’t be [1,2,3,0,4]?

1 answer

0

lista = [6,4,3,9,1]

#lista com os valores em ordem decrescentes e sem valores repetidos
lista_decrescente = list(set(lista))
lista_decrescente.sort(reverse=True)

lista_indicies = [None]*len(lista)

indicie = 0
for num in lista_decrescente:
    for i in range(0, len(lista)):
        if lista[i] == num:
            lista_indicies[i] = indicie
            indicie += 1

>>> print(lista_indicies)
[1, 2, 3, 0, 4]
  • That’s right, thank you for the answer, yesterday I managed to do otherwise, only that so ta much simpler, thanks for taking my doubt

Browser other questions tagged

You are not signed in. Login or sign up in order to post.