Python dictionary Alphabetical order and duplicate existing dictionary

Asked

Viewed 159 times

-1

Personal I am in doubt on how to write on the screen for the user the values of the dictionary without the bracket and in alphabetical order. I am also doubtful on how to make two new dictionaries to select only contacts aged 18 or older and the other aged 18 or less.

The following is the statement of the question: Read and store in a dictionary the name, age and phone number of your contacts, the key being the name. When typing an empty string for the name, the program stops reading and closes. Display on the screen the data read in alphabetical order by the name of the contacts. A possible solution to sort alphabetically is to use the Sort method. Then store the contacts in two other dictionaries, using as age criterion: under 18 years in one and the largest in another dictionary, eliminating the original. Present on the screen the two dictionaries resulting from the separation.

My code:

from operator import itemgetter

nomesContatos = {}

for i in range(0, 300):
    nome = input('Digite o nome do contato: ')
    nome = nome.capitalize()
    grade = []

    if nome == '':
        break

    for j in range(0, 1):
        while True:
            try:
                idade = int(input('Digite a idade: '))
                grade.append(idade)
                break
            except ValueError:
                print('Idade inválida...')
                continue

        for k in range(0, 1):
            while True:
                try:
                    telefone = int(input('Digite o telefone: '))
                    grade.append(telefone)
                    break
                except ValueError:
                    print('Número inválido...')

    nomesContatos[nome] = grade
    print('\n')


for contatos in sorted(nomesContatos, key=nomesContatos.get):
    a = (nomesContatos[contatos])

    print(a)

1 answer

1

Personal I am in doubt on how to write on the screen for the user the values of the dictionary without the bracket and in alphabetical order.

You can give Sort in the dictionary keys and then interact key the key:


nomes=  {
    "Joao":[1,11999999],
    "Maria":[5,21123451],
    "Ana":[6,4123212451],
}

ordem_alfabetica = sorted(nomes.keys())
ordem_alfabetica

#output
>> ['Ana', 'Joao', 'Maria']

for nome in ordem_alfabetica:
  print(f"{nome} --->  Nota : {nomes.get(nome)[0]} ---> telefone : {nomes.get(nome)[1]}")

#ouput
>> Ana --->  Idade: 6 ---> telefone : 4123212451
>> Joao --->  Idade: 1 ---> telefone : 11999999
>> Maria --->  Idade: 5 ---> telefone : 21123451

I’m also doubting how to make two new dictionaries for select only the contacts with 18 years or older and the other with 18 years or less.

it is not necessary to make 2 dictionaries for that you can put a condition and treat case by case, so if you want you can create 2 dictionaries tbm

menor = {}
maior = {}
for nome in ordem_alfabetica:

  if nomes.get(nome)[0] < 18:
    print("Eu tenho menos de 18")
    menor[nome] = nomes[nome]
    #Do Stuff
  else:
    print("Eu tenho 18 ou mais")
    maior[nome] = nomes[nome]
    #Do Stuff

Browser other questions tagged

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