1
I have lists of sublists and tuples inside. I need to create a dictionary whose key is the car name and the value is a dictionary nested with the car model count. My lists are like this (it’s just a small example, with partial result I counted in the eye!):
navio_fatiado = [[('Ferrari', 'CONV'), ('Audi', 'SEDAN')],[('Fusca', 'CONV'), ('Limousine', 'VINTAGE')], [('Porsche', 'CONV'), ('Lamborghini', 'CONV')],[('Audi', 'CONV'), ('Fusca', 'CONV')]]
resultado_desejado = {'Ferrari': {'CONV': 1}, 'Audi': {'SEDAN': 1, 'CONV': 1}, 'Fusca': {'CONV': 2}, 'Limousine': {'VINTAGE': 1}}
So far, all I’ve been able to do is this:
from collections import defaultdict
dicionario_chave = defaultdict(int)
dicionario_valor = defaultdict(int)
for linhas in navio_fatiado:
for elementos in linhas:
if not elementos[0] in dicionario_chave:
dicionario_chave[elementos[0]]
if elementos[1] in dicionario_valor:
dicionario_valor[elementos[1]] += 1
else:
dicionario_valor[elementos[1]] = 1
I mean, I can only count the values, but I can’t put together the dictionaries with their correct keys, which are the other dictionaries.
Output:
print(dicionario_chave, dicionario_valor) defaultdict(<class 'int'>, {'Ferrari': 0, 'Audi': 0, 'Fusca': 0, 'Limousine': 0, 'Porsche': 0, 'Lamborghini': 0}) defaultdict(<class 'int'>, {'CONV': 4, 'SEDAN': 1, 'VINTAGE': 1})
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