The easiest way is to use one Counter
, standard library class collections
.
It acts as a dictionary and can be initialized with a list, or in this case a generating expression:
from collections import Counter
# especie,nome
listaAnimais = [
('leao', 'Simba'),
('javali', 'Pumba'),
('leao', 'Scar'),
('hiena', 'Banzai'),
('leao', 'Mufasa'),
]
# Criar um contador, o inicializando com a primeira string
# de cada elemento em `listaAnimais`
dicEspecies = Counter(animal[0] for animal in listaAnimais)
print(dicEspecies)
# Counter({'leao': 3, 'javali': 1, 'hiena': 1})
Behind the scenes, this solution is efficient because it runs in time linearly proportional to the size of the list, and not quadratically how to use count
within a loop for
.
In the simplest way possible, she does the equivalent of this:
# Inicializamos um dicionário vazio
dicEspecies = {}
# Para cada animal...
for especie, nome in listaAnimais:
# Se a espécie ainda não estiver no nosso
# dicionário, a inicializamos com o valor de 0
if especie not in dicEspecies:
dicEspecies[especie] = 0
# Em qualquer caso, somamos um à contagem
# dessa espécie
dicEspecies[especie] += 1
print(dicEspecies)
# {'leao': 3, 'javali': 1, 'hiena': 1}
About performance: it is a trivial observation for a list of this size, but if the list were large, we would have to be careful with the complexity of the algorithm used for counting. That answer has interesting content on the complexity of algorithms and how to analyze it. That answer is also very good.
Basically, the approach using count
inside the loop for
works in quadratic time O(N 2): that is, as the size of the list N increases, the execution time of the function increases proportionally to N 2.
This is because
- The algorithm iterates under all elements of the list with the
for
- For every element, he calls
count
: this function, internally, again traverses the entire list to count how many elements are present.
Already using a dictionary or Counter, we only go through the list once on for
. The two, internally, are a data structure called a hashmap (hashmap), which has the property of having constant-time access and belonging verification; that is, it is independent of the list size N.
In this case, as the list size N grows, the function runtime increases proportionally to N, because we only go through the list once, not N times.
Welcome to Sopt. I edited your question so that it meets the standards of the site. To improve your future questions, please read this manual: https://pt.meta.stackoverflow.com/questions/5483/manual-de-como-n%C3%83o-ask-questions? cb=1
– Lucas
If one of the answers below solved your problem and there was no doubt left, choose the one you liked the most and mark it as correct/accepted by clicking on the " " that is next to it, which also marks your question as solved. If you still have any questions or would like further clarification, feel free to comment.
– Lucas