List of 3 positions. Each one contains a list of two positions both integers. Sort by largest integer of 2nd position

Asked

Viewed 25 times

0

listaSupermercados1 = [[1, 10], [2, 20], [1, 15]]
def comprarBananas(listaSupermercados):
    listaSupermercados.append(listaSupermercados)
    comprimentoLista = len(listaSupermercados) -1
    posicao = 0
    posica1 = 1
    posicaoSubsequente = posica1 + 1
    listaOrganizada = []
    while posicao < (comprimentoLista):
        valorUnitarioPosicaoInicial = listaSupermercados[posicao][posica1]
        valorUnitarioPosicaoSubsequente = listaSupermercados1[posicaoSubsequente][posica1]
        if valorUnitarioPosicaoInicial < valorUnitarioPosicaoSubsequente:
            listaOrganizada.append(listaSupermercados[posicao])
        else:
            listaOrganizada.append(listaSupermercados[posicaoSubsequente])
        posicao += 1
        posicaoSubsequente = + 1
    print(listaOrganizada)

comprarBananas(listaSupermercados1)

Each sub-list has 2 positions with integers and I want to create a new list in ascending order by the value of each element in the 2nd position of the sub-lists But I get the mistake:

ERROR: if valueUnitesPositionInitial < valueUnitesSubscribing: Typeerror: '<' not supported between instances of 'list' and 'int'

  • BAH doesn’t even post right I can :-(

  • Your code is not showing error, but it is not doing what it should. In a pythonic way, you could do something like: sorted(listaSupermercados1, key=lambda x: x[1], reverse=True)

  • Hi Paulo Thanks! I couldn’t understand your code (I’m new in Python) I could explain how I put it in my code?

  • Got it ! Thank you very much

  • listSupermercados1 = [[1, 10], [2, 20], [1, 15]] def buyBananas(listSupermercados): listOrganizada = Sorted(listSupermercados, key=lambda x: x[1]) print(listOrganized) &#x;buyBananas(listSupermercados1)

No answers

Browser other questions tagged

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