Am 09.03.2010 13:02, schrieb Peter Otten:
[sum(a for a,b in zip(x,y) if b==c)/y.count(c)for c in y]
[1.5, 1.5, 8.0, 4.0, 4.0, 4.0]
Peter
... pwned.
Should be the fastest and shortest way to do it.
I tried to do something like this, but my brain hurt while trying to
visualize list
OK, I golfed it :D
Go ahead and kill me ;)
x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]
def f(a,b,v={}):
try: v[a].append(b)
except: v[a]=[b]
def g(a): return sum(v[a])/len(v[a])
return g
w = [g(i) for g,i in [(f(i,v),i) for i,v in zip(y,x)]]
print(w is now the
Michael Rudolf wrote:
OK, I golfed it :D
Go ahead and kill me ;)
x = [1 ,2, 8, 5, 0, 7]
y = ['a', 'a', 'b', 'c', 'c', 'c' ]
def f(a,b,v={}):
try: v[a].append(b)
except: v[a]=[b]
def g(a): return sum(v[a])/len(v[a])
return g
w = [g(i) for g,i in [(f(i,v),i) for
Michael Rudolf wrote:
Am 09.03.2010 13:02, schrieb Peter Otten:
[sum(a for a,b in zip(x,y) if b==c)/y.count(c)for c in y]
[1.5, 1.5, 8.0, 4.0, 4.0, 4.0]
Peter
... pwned.
Should be the fastest and shortest way to do it.
It may be short, but it is not particularly efficient. A dict-based
Peter Otten wrote:
Michael Rudolf wrote:
Am 09.03.2010 13:02, schrieb Peter Otten:
[sum(a for a,b in zip(x,y) if b==c)/y.count(c)for c in y]
[1.5, 1.5, 8.0, 4.0, 4.0, 4.0]
Peter
... pwned.
Should be the fastest and shortest way to do it.
It may be short, but it is not