formatting values of a dictionary

Asked

Viewed 83 times

2

tamanho = int(input())
agenda = {}
cont = 0
for c in range(tamanho):
    cont += 1
    nome = str(input()).strip().lower().capitalize()
    cidade = str(input()).lower().capitalize()
    estado = str(input()).upper()
    telefone = input()
    agenda[cont] = nome,cidade,estado,telefone

for v in agenda.values():
    print(f'{v[0]:10}{v[1]}{v[2]:10}{v[3]:10}')

I do not know how to make a more coherent format than the one I did ,of these values of the dictionary, but if there is a better way to do this, why gentiliza could mi give some ideas of how it does.

Input examples:

4
Gael
São Luís
MA
97824-4673
Vitória
Manaus
AM
98084-8437
Alícia
Brasília
DF
99114-8269
Clara
Duque de Caxias
RJ
97906-7003

That’s the way I was hoping:

Gael                     São Luís(MA)                  97824-4673
Vitória                  Manaus(AM)                    98084-8437
Alícia                   Brasília(DF)                  99114-8269
Clara                    Duque de Caxias(RJ)           97906-7003
  • You can use the methods rjust and ljust Behold here

1 answer

2


First, if the dictionary keys are 1, 2, 3, etc., there is not much advantage in using a dictionary. When keys are sequential numbers, this is a strong indication that maybe what you need is a list.

And to format, you can use the formatting options, as it is already doing. The difference is that the phone is aligned right, so just do >tamanho, thus:

tamanho = int(input())
agenda = []
for _ in range(tamanho):
    nome = input().strip().lower().capitalize()
    cidade = input().lower().capitalize()
    estado = input().upper()
    telefone = input()
    # insere a tupla na lista
    agenda.append((nome,cidade,estado,telefone))

for nome, cidade, estado, telefone in agenda:
    # junta cidade e estado (com o estado entre parênteses)
    cid_uf = f'{cidade}({estado})'
    # nome alinhado à esquerda com 20 posições
    # cidade e estado alinhado à esquerda com 20 posições
    # telefone alinhado à direita com 20 posições
    print(f'{nome:20}{cid_uf:20}{telefone:>20}')

Also note that in the for I can assign the values of the tuple directly to specific variables, so it makes it easier to manipulate them.

And see that to insert the tuple in the list I need extra parentheses: agenda.append((nome,cidade,estado,telefone)).
If I just do agenda.append(nome,cidade,estado,telefone), will give error, because then I will be passing 4 arguments to append. With the extra parentheses, I pass only 1 argument, which is the tuple containing the 4 information.

I joined the city and state before because it seems to me that they are part of the same column, so joining them makes it easier to format later.

And I put the size equal to 20 because the phone already has 10 characters, so using the size equal to 10 will not make a difference in formatting. But then you can adjust the values to whatever you need.

Another detail is that input already returns a string, so do str(input()) is redundant and unnecessary.

The exit is:

Gael                São luís(MA)                  97824-4673
Vitória             Manaus(AM)                    98084-8437
Alícia              Brasília(DF)                  99114-8269
Clara               Duque de caxias(RJ)           97906-7003

Browser other questions tagged

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