groupby - agrupando valores (itertools)
This commit is contained in:
27
aula110.py
Normal file
27
aula110.py
Normal 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)
|
||||||
Reference in New Issue
Block a user