No need to complicate creating 2 dictionaries (one with the names and the other with the counts). Do it all at once:
navio_fatiado = [
[('Ferrari', 'CONV'), ('Audi', 'SEDAN')],
[('Fusca', 'CONV'), ('Limousine', 'VINTAGE')],
[('Porsche', 'CONV'), ('Lamborghini', 'CONV')],
[('Audi', 'CONV'), ('Fusca', 'CONV')]
];
result = {}
for container in navio_fatiado:
for carro, modelo in container:
if carro not in result: # carro ainda não está no dicionário
result[carro] = {}
if modelo not in result[carro]: # modelo ainda não está no dicionário do carro
result[carro][modelo] = 1
else: # modelo já existe, atualiza a contagem
result[carro][modelo] += 1
print(result)
Note that when going through the tuples that contain the name of the car and model, I can assign the values to variables in the for (in for carro, modelo in container). That is, in the first iteration of the first list, nome will be "Ferrari" and modelo will be "CONV" in the second iteration, nome shall be "Audi" and modelo will be "SEDAN", etc.
Then I’ll see if the car isn’t in the main dictionary yet (if it isn’t, I’ll create an empty dictionary for it).
Then I see if the model is in the dictionary of the car, and if it is not, I put the value 1 (and if it is, we add 1 to the current value).
The result will be:
{'Ferrari': {'CONV': 1}, 'Audi': {'SEDAN': 1, 'CONV': 1}, 'Fusca': {'CONV': 2}, 'Limousine': {'VINTAGE': 1}, 'Porsche': {'CONV': 1}, 'Lamborghini': {'CONV': 1}}
Another alternative to count is to use a Counter:
from collections import Counter
navio_fatiado = [
[('Ferrari', 'CONV'), ('Audi', 'SEDAN')],
[('Fusca', 'CONV'), ('Limousine', 'VINTAGE')],
[('Porsche', 'CONV'), ('Lamborghini', 'CONV')],
[('Audi', 'CONV'), ('Fusca', 'CONV')]
];
result = {}
for container in navio_fatiado:
for carro, modelo in container:
if carro not in result: # carro ainda não está no dicionário
result[carro] = Counter() # cria um Counter vazio
result[carro].update([modelo])
print(result)
The idea is similar, the difference is that the Counter already takes care of the details of updating the count of each model.
The exit is a little different:
{'Ferrari': Counter({'CONV': 1}), 'Audi': Counter({'SEDAN': 1, 'CONV': 1}), 'Fusca': Counter({'CONV': 2}), 'Limousine': Counter({'VINTAGE': 1}), 'Porsche': Counter({'CONV': 1}), 'Lamborghini': Counter({'CONV': 1})}
But a Counter is a subclass of dict and so it is also a dictionary and can be used as such (for example, to know how many Ferrari CONV there are, just do result['Ferrari']['CONV']), exactly as you would with dictionaries).
Even as the exit from print not exactly the same, what matters - in my opinion - is that the count is correct. Another point is that a Counter allows other operations that may be useful (if you wish), such as knowing which are the most frequent models. But anyway, here are the solutions, see which is the most suitable for your case.
I’m sorry but because I rejected the issue. I was just making the question clearer before submitting an answer.
– Augusto Vasques
I reversed to a format more suitable to the site.
– Augusto Vasques
It’s the first time I use the site and I’m learning. I didn’t know I had revisions, and I didn’t agree with all of them. I appreciate your kindness anyway. I will certainly stay tuned in the next.
– José Carlos
I’m sorry to sound rude, but I’m just clarifying the text normative site. Understand how the site works by reading What is Stack Overflow. Here are some guidelines that will help you: Stack Overflow Survival Guide in English
– Augusto Vasques
Sir, texts like
Alguém poderia me ajudar? Obrigado.andAinda sou iniciante no Python e não consigo resolver um problema.are considered communication noises here on the site. Read the links I passed.– Augusto Vasques