1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
| >>> ListNotes = [['Mathématiques', '12'], ['Mathématiques', '9'], ['Mathématiques', '15'], ['Informatique', '17'], ['Physique', '11'], ['Physique', '8'], ['Chimie', '16'], ['Chimie', '10']]
>>> c = list(zip(*sorted(ListNotes))) #[('Chimie', 'Chimie', 'Informatique', 'Mathématiques', 'Mathématiques', 'Mathématiques', 'Physique', 'Physique'), ('10', '16', '17', '12', '15', '9', '11', '8')]
>>> new_liste=[[None,None]]
>>> def mean(liste):
z = 0
for x in liste:
z+=float(x)
return z/len(liste)
>>> for i,x in enumerate(c[0]):
if x not in [y for y,z in new_liste]:
new_liste.append([x,mean(c[1][i:i+c[0].count(x)])])
>>> dict(new_liste[1:])
{'Mathématiques': 12.0, 'Informatique': 17.0, 'Physique': 9.5, 'Chimie': 13.0} |
Partager