groupby - agrupando valores (itertools)

This commit is contained in:
Luiz Otávio
2022-11-06 11:38:31 -03:00
parent 1b230f0f0c
commit 6227dc9197

27
aula110.py Normal file
View File

@@ -0,0 +1,27 @@
# groupby - agrupando valores (itertools)
from itertools import groupby
alunos = [
{'nome': 'Luiz', 'nota': 'A'},
{'nome': 'Letícia', 'nota': 'B'},
{'nome': 'Fabrício', 'nota': 'A'},
{'nome': 'Rosemary', 'nota': 'C'},
{'nome': 'Joana', 'nota': 'D'},
{'nome': 'João', 'nota': 'A'},
{'nome': 'Eduardo', 'nota': 'B'},
{'nome': 'André', 'nota': 'A'},
{'nome': 'Anderson', 'nota': 'C'},
]
def ordena(aluno):
return aluno['nota']
alunos_agrupados = sorted(alunos, key=ordena)
grupos = groupby(alunos_agrupados, key=ordena)
for chave, grupo in grupos:
print(chave)
for aluno in grupo:
print(aluno)