Basically you should go through each item of the list checking whether the position relative to the profession contains "Doctor" or else "Teacher" and incrementing the respective counter. Something like that:
lista_teste = [
['Nome','Idade','Sexo','Profissão'],
['Pedro','25','Masculino','Médico'],
['José','36','Masculino','Professor'],
['Paula','47','Feminino','Policial'],
['Pedro','58','Masculino','Professor'],
['Marcos','49','Masculino','Médico'],
['Daniela','30','Feminino','Professor'],
['Heitor','21','Masculino','Médico'],
['Carlos','32','Masculino','Professor'],
['Alice','43','Feminino','Médico'],
['Ricardo','54','Masculino','Mecânico'],
['Maria','65','Feminino','Professor'],
]
medicos = 0
professores = 0
for pessoa in lista_teste[1:]:
if pessoa[3] is 'Médico':
medicos += 1
elif pessoa[3] is 'Professor':
professores +=1
print('Total de {} médico(s) e de {} Professor(es).'
.format(medicos, professores))
What the for
makes iterate with each element of the lisa 'list_test' from the 2nd item (the "[1:]" that say from the 2nd element to the end, the list starts at 0) putting its content in the variable 'person', which is also a list.
Hence it is to verify the 4th element of this checking the content of the 4th element, the profession, and taking care to increase 'doctors' or 'teachers' depending on the case. Notice that there are two professions that are not counted, I left them on purpose so you can take your tests.
Of course, this is a much more didactic example than practical.
In case I need to create a function to add the columns of a list in another list, and that is in the same order, as I could perform this function.
– R.Villela
Hi, just use the method
.append()
and add the items in the new row, for example:lista_teste.append(['Alice','43','Feminino','Médico'])
.– Giovanni Nunes