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,
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
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
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
--
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
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
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:
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
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
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
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
11 matches
Mail list logo