1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39
| from time import clock
from itertools import groupby
d = {'Ei': (1,3,4,4,6,70,80,2000,3000,4000,6000,1111,2222,
23,13,43,53,63,733,93,83,13,33,65,75,85) ,
'id' : ('r','r','t','t','t','r','r','b','b','b','b','x','y',
'a','a','a','a','a','a','a','a','a','a','r','r','r')}
to = clock()
for x in xrange(10000):
result = []
# Retourne les éléments associés à 'r' et à 't'
for key, group in groupby(zip(d['id'], d['Ei']), lambda x: x[0]):
# Pour 'r' et 't' on ajoute les éléments retourner par le groupage
gr = tuple(group)
result.append((key, sum(thing[1] for thing in gr)/len(gr)))
dt = clock()-t0
print dt
print result
print '\n'
def f_moy(d):
jprec,idprec = 0,d['id'][0]
for j,idx in enumerate(d['id']):
if idx!=idprec:
y = idprec,sum(d['Ei'][jprec:j])/(j-jprec)
jprec,idprec = j,idx
yield y
j+=1
yield d['id'][-1],sum(d['Ei'][jprec:j])/(j-jprec)
t0 = clock()
for x in xrange(10000):
resf = f_moy(d)
dt = clock()-t0
print dt
print [u for u in f_moy(d)] |
Partager