Sort an alphanumeric list by number

Asked

Viewed 178 times

-1

For study purposes, I have the following problem:

A list with grocery shopping with description and values , I need to order increasingly but without losing the description.

Note: To order the items I have to create the functions, I can not use ready.

Example:

Lista = ["pao" , "2.50" , "queijo" , "3.00", "leite", "2.00"]
Lista_ordenada = ["leite","2.00" ,"pao" , "2.50" , "queijo" , "3.00"]

I thought I’d use Bubble Sort always comparing even indexes, but I could not.

  • 1

    But you have a list of strings where some are descriptions and others are values? At the very least, I would say that it is a very strange structure. Probably a list of tuples would better represent your data. You defined that list or got it from somewhere?

  • is an exercise, I have to report the description and the value. I know I could use dictionary, key and value, but in that case I can’t, I have to do it using list only.

  • 1

    Then change the structure that stores the data. Putting data of different natures in the same list will only add complexity to your problem and decrease semantics/readability/maintainability of your application

  • Anderson, I understand that, , but it’s an exercise I’ve been given , and I wanted to solve it. know that in a real application, I won’t use it that way.

  • Then put the whole exercise statement in the question so that the answers can bring you the best possible solution (and not just a solution from what you have already done)

1 answer

3


First you have to join the values with their descriptions. The list, like this, does not have a mapping between product and value. You could for example make tuples of two items: the product and its value.

minha_lista = ["pao" , "2.50" , "queijo" , "3.00", "leite", "2.00"]
lista_pares = []
for i in range(0, len(minha_lista), 2):  # Iterar sob a lista em passos de 2
    item = minha_lista[i]
    preco = minha_lista[i + 1]
    lista_pares.append((item, preco))

print(lista_pares)
# [('pao', '2.50'), ('queijo', '3.00'), ('leite', '2.00')]

Now, yes, you can use sorted:

lista_pares_ordenada = sorted(lista_pares, key=lambda tupla: float(tupla[1]))
print(lista_pares_ordenada)
# [('leite', '2.00'), ('pao', '2.50'), ('queijo', '3.00')]

And to turn the list back into a single-string format:

lista_ordenada = []
for tupla in lista_pares_ordenada:
    lista_ordenada.append(tupla[0])
    lista_ordenada.append(tupla[1])

print(lista_ordenada)
# ['leite', '2.00', 'pao', '2.50', 'queijo', '3.00']
  • "Note: To order the items I have to create the functions, I can not use ready." I don’t think he’ll be able to use the sorted :(

  • Thank you Pedro, I can’t use tuples , just list , and I can’t use any of them.. , but this already gives me some ideas. Thank you very much

  • Jeez, not even tuples? Very strange exercise...

  • @Oh Evertonj, I didn’t see the remark, sorry. But the big thing here is to group the consecutive list items in pairs, it can be in other lists even instead of tuples, and then sort by the second item.

Browser other questions tagged

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