I think more important than the algorithm, you need to understand that the structure you chose for your data is not suitable. I’ll try to make a comparison here to see if it’s clear how much better:
You want to save information from several people, for this you have made a dictionary that contains lists with the information:
pessoas = {
"nomes": ["Fulano", "Beltrano", "Sicrano"],
"idades": [10, 20, 30],
}
If you were to represent this physically, you would have a book that contains only one person’s name per page and another book with only the ages on the pages. So to know the age of "Beltrano", you would have to open the book of names, find the page on which the name is and search the same page in the book of ages.
As you can see this is not practical. A more correct structure would be to have all the information you want from a person on the same page. In this example you would create a dictionary list instead of a list dictionary.
See the difference:
pessoas = [
{"nome": "Fulano", "idade": 10},
{"nome": "Beltrano", "idade": 20},
{"nome": "Sicrano", "idade": 30},
]
That way, when you access pessoas[0]
you’re seeing all that person’s information on a "page" only.
To sort the list of dictionaries just pass the parameter key
for the function sorted
to "teach" the function how to take the value by which you want to order.
pessoas_ordenado = sorted(pessoas, key=lambda obj: obj["nome"])
for pessoa in pessoas_ordenado:
print(f"{pessoa['nome']} tem {pessoa['idade']} anos.")
Exit:
Beltrano tem 20 anos.
Fulano tem 10 anos.
Sicrano tem 30 anos.
Code running on Repl.it
The function sorted
returns a new sorted list, if you do not need to save this separates list you can use the method list.sort()
ordination inplace, that is, changes the original list.
Using list.sort()
would look like this:
pessoas.sort(pessoas, key=lambda obj: obj["nome"])
for pessoa in pessoas:
print(f"{pessoa['nome']} tem {pessoa['idade']} anos.")
I used dictionaries as an example to leave information of people in the same structure, but it could be any other structure as objects of a class Pessoa
, tuples, named tuples, etc..
Some examples just for reference:
Classes:
class Pessoa:
def __init__(self, nome, idade):
self.nome = nome
self.idade = idade
pessoas = [
Pessoa(nome="Fulano", idade=10),
Pessoa(nome="Beltrano", idade=20),
Pessoa(nome="Sicrano", idade=30),
]
pessoas_ordenado = sorted(pessoas, key=lambda obj: obj.nome)
for pessoa in pessoas_ordenado:
print(f"{pessoa.nome} tem {pessoa.idade} anos.")
Named tuples:
Pessoa = namedtuple('Pessoa', ['nome', 'idade'])
pessoas = [
Pessoa(nome="Fulano", idade=10),
Pessoa(nome="Beltrano", idade=20),
Pessoa(nome="Sicrano", idade=30),
]
pessoas_ordenado = sorted(pessoas, key=lambda obj: obj.nome)
for pessoa in pessoas_ordenado:
print(f"{pessoa.nome} tem {pessoa.idade} anos.")
Dude, it was right...very obg! I’ve been racking my brain for a while now.... vlw msm xD
– Henrique Marvin Souza da Silva
@Henriquemarvinsouzadasilva You’re welcome! If the answer pleases you, don’t forget to accept it to help future site searches.
– Naslausky