String occurrence calculation in list - Python

Asked

Viewed 343 times

2

Staff need a help to calculate the amount of occurrences of a given string in a column of a list. Example:

lista_teste=['Name','Age','Sex','Profession'],['Peter','25','Male','Physician']...]

Ex: Calculate number of occurrences per profession---------

doctor=0

teacher=0

How to count using FOR or not using FOR

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

2 answers

4

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

from collections import Counter

After that 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 part of picking only the professions, the first element of the list was ignored which was the header and corresponds to the lista_teste[1:] from the position 1 at the end. And for each person was captured the fourth column which is the column in the position 3 with pessoa[3] corresponding to the profession.

See this example in Ideone

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

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

Which means you can make the code more dynamic if instead of fixing it Professor and Médico use a for to show all the totalised 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

0

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.

  • Hi, just use the method .append() and add the items in the new row, for example: lista_teste.append(['Alice','43','Feminino','Médico']).

Browser other questions tagged

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