Re: Inefficient summing

2008-10-10 Thread Alexander Schmolck
beginner [EMAIL PROTECTED] writes: On Oct 9, 3:53 pm, Alexander Schmolck [EMAIL PROTECTED] wrote: beginner [EMAIL PROTECTED] writes: how about: ratio = (lambda c: c.real/c.imag)(sum(complex(r[F1], r[F2] for r in rec))) Neat, but I will have a problem if I am dealing with three fields,

Re: Inefficient summing

2008-10-09 Thread Matt Nordhoff
Chris Rebert wrote: I personally would probably do: from collections import defaultdict label2sum = defaultdict(lambda: 0) FWIW, you can just use: label2sum = defaultdict(int) You don't need a lambda. for r in rec: for key, value in r.iteritems(): label2sum[key] += value

Re: Inefficient summing

2008-10-09 Thread bieffe62
On 8 Ott, 22:23, beginner [EMAIL PROTECTED] wrote: Hi All, I have a list of records like below: rec=[{F1:1, F2:2}, {F1:3, F2:4} ] Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is: sum(r[F1] for r in rec)/sum(r[F2] for r in rec) But

Re: Inefficient summing

2008-10-09 Thread bearophileHUGS
FB: def add_r( sums, r ): return sums[0]+r['F1'], sums[1]+r['F2'] sum_f1, sum_f2 = reduce( add_r, rec, (0,0) ) result = sum_f1/sum_f2 Until this feature vanishes I think it's better to use it (untested): add_r = lambda (a, b), r: (a + r['F1'], b + r['F2']) Bye, bearophile --

Re: Inefficient summing

2008-10-09 Thread Terry Reedy
Matt Nordhoff wrote: Chris Rebert wrote: I personally would probably do: from collections import defaultdict label2sum = defaultdict(lambda: 0) FWIW, you can just use: label2sum = defaultdict(int) You don't need a lambda. Indeed, in this case, with two known keys, the defaultdict is not

Re: Inefficient summing

2008-10-09 Thread Alexander Schmolck
beginner [EMAIL PROTECTED] writes: Hi All, I have a list of records like below: rec=[{F1:1, F2:2}, {F1:3, F2:4} ] Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is: sum(r[F1] for r in rec)/sum(r[F2] for r in rec) But this is slow

Re: Inefficient summing

2008-10-09 Thread beginner
On Oct 9, 3:53 pm, Alexander Schmolck [EMAIL PROTECTED] wrote: beginner [EMAIL PROTECTED] writes: Hi All, I have a list of records like below: rec=[{F1:1, F2:2}, {F1:3, F2:4} ] Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is:

Inefficient summing

2008-10-08 Thread beginner
Hi All, I have a list of records like below: rec=[{F1:1, F2:2}, {F1:3, F2:4} ] Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is: sum(r[F1] for r in rec)/sum(r[F2] for r in rec) But this is slow because I have to iterate through the list

Re: Inefficient summing

2008-10-08 Thread Bruno Desthuilliers
beginner a écrit : Hi All, I have a list of records like below: rec=[{F1:1, F2:2}, {F1:3, F2:4} ] Now I want to write code to find out the ratio of the sums of the two fields. One thing I can do is: sum(r[F1] for r in rec)/sum(r[F2] for r in rec) But this is slow because I have to iterate

Re: Inefficient summing

2008-10-08 Thread Chris Rebert
I personally would probably do: from collections import defaultdict label2sum = defaultdict(lambda: 0) for r in rec: for key, value in r.iteritems(): label2sum[key] += value ratio = label2sum[F1] / label2sum[F2] This iterates through each 'r' only once, and (imho) is pretty

Re: Inefficient summing

2008-10-08 Thread bearophileHUGS
beginner: I can of course use an old-fashioned loop. This is more readable, but also more verbose. What is the best way, I wonder? In such situation the old loop seems the best solution. Short code is good only when it doesn't make the code too much slow/difficult to understand. Keeping the