How do I alphabetize the Telephonic list that is separated between older and younger?

Asked

Viewed 58 times

-1

Before making a Phonebook containing name, phone and age, after capturing the data, I need to separate them into older and younger 18 years and print on the screen the result with all the data registered, I’m hitting me to be performed alphabetical order. Could you help me?

agenda = []
dados = {}
maior = []
menor = []
dados['nome'] = input('Digite o Nome: ')


while dados['nome'] != '':
    dados['idade'] = int(input('Digite a Idade: '))
    dados['telefone'] = int(input('Digite o Telefone: '))
    agenda.append(dados.copy())
    dados['nome'] = str(input('Digite o Nome: '))

print('')
print('Total de Cadastros')
print(f'{"Nome":<15}{"Idade":<15}{"Telefone":<15}')
print('')
for contatos in agenda:
    print(f'{contatos["nome"]:<15}{contatos["idade"]:<15}{contatos["telefone"]:<15}')
    if contatos['idade'] >= 18:
        maior.append(contatos)
    if contatos['idade'] < 18:
        menor.append(contatos)
print('-'*50)
print('Maiores de Idade')
print(f'{"Nome":<15}{"Idade":<15}{"Telefone":<15}')
print('')
for contatoslista in maior:
    dados = dict(sorted(dados.items(), key=lambda chave: chave[0]))
    print(f'{contatoslista["nome"]:<15}{contatoslista["idade"]:<15}{contatoslista["telefone"]:<15}')
print('-'*50)
print('Menores de Idade')
print(f'{"Nome":<15}{"Idade":<15}{"Telefone":<15}')
print('')
for contatoslista in menor:
    print(f'{contatoslista["nome"]:<15}{contatoslista["idade"]:<15}{contatoslista["telefone"]:<15}')
  • The structure you chose for your data is not ideal, in that reply I explain a little bit about this.

1 answer

2

The data structure you currently have for storing contacts will only be able to store a single contact. In the first while loop, after filling the first contact, the second loop iteration will override the contact.

To solve this you can use a list of dictionaries, where each dictionary is a contact. At the end of while, you add at the end of the list.

lsita_de_contatos = []

while dados['nome'] != '':

    contato = {}

    contato['idade'] = int(input('Digite a Idade: '))
    contato['telefone'] = int(input('Digite o Telefone: '))
    contato['nome'] = str(input('Digite o Nome: '))

    lista_de_contatos.append(contato)

To sort the contacts by alphabetical order of the names, you can do:

contatos_por_ordem_alfabetica = sorted(lista_de_contatos, key=lambda k: k['nome'])

Where lambda is a small function that simply serves to collect the field that will be used in the ordering comparison, which in this case is the name field.

Finally, to find contacts that respect a given condition, you can make a list comprehension that will filter the contact list:

 maiores_de_idade = [contato for contato in lista_de_contatos if contato['idade'] > 18]

These tips already help you solve all the rest of the problem. Remember to keep the code and ideas organized, so you will arrive at the solution you need. Good luck, I hope to have helped!

  • Thanks for the collaboration, I am starting in this world and it has not been easy, I will put into practice your tips, thank you very much

  • Arrange. Welcome to the python community!

Browser other questions tagged

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