Calculation of occurrence of strings in list - Python

1

Personal I need a help to calculate the number of occurrences of a given string in a column of a list. Example:

test_list = [['Name', 'Age', 'Sex', 'Occupation'], ['Peter', '25' em>

Ex: Calculate the number of occurrences by profession ---------

doctor = 0

teacher = 0

How to count using FOR or not using FOR

print ("Doctors=", doctor, "Teachers" =, teacher)

    
asked by anonymous 06.05.2018 / 03:01

2 answers

3

A good way to count is to use the Counter class that already exists in collections for this purpose. This makes it necessary to import it before using:

from collections import Counter

After this and having the list defined, you can do everything with 3 lines:

profissoes = [pessoa[3] for pessoa in lista_teste[1:]] # apanhar só as profissões
totais = Counter(profissoes) # totalizar
print("Medicos={} Professores={}".format(totais['Professor'], totais['Médico'])) # mostrar

In the Collect Only Professions section, the first element in the list that was the header was ignored and corresponds to the lista_teste[1:] that picks up from the 1 position to the end. And for each person was captured the fourth column which is the column in position 3 with pessoa[3] corresponding to the profession.

See this example in Ideone

It is important to note that only the totalization made with Counter(profissoes) gives you a dictionary with all the existing professions and their counts:

Counter({'Professor': 3, 'Médico': 2})

What means you can leave the code more dynamic if instead of indicating Professor and Médico use a for to show all the totaled professions:

profissoes = [pessoa[3] for pessoa in lista_teste[1:]] # apanhar só as profissões
totais = Counter(profissoes) # totalizar
for profissao,total in totais.items(): # percorrer cada profissão e total
    print("{}={}".format(profissao, total)) # mostrar

See also this example in Ideone

    
06.05.2018 / 03:54
0

Basically you should go through each item in the list by checking if the position relative to the profession contains "Doctor" or "Teacher" and incrementing the respective counter. Something like this:

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 for does is iterate with each element of the flat 'test_list' from the 2nd item (the '[1:]' from the 2nd element to the end, the list starts at 0) content in the 'person' variable, which is also a list.

From there, check 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 that you can do your tests.

Of course, this is a much more didactic than practical example.

    
06.05.2018 / 03:55