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:
>
> > 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 twice.
> > Also, in the case where rec is an iterator, it does not work.
>
> how about:
>
> ratio = (lambda c: c.real/c.imag)(sum(complex(r["F1"], r["F2"] for r in rec)))
>
> ?
>
> :)- Hide quoted text -
>
> - Show quoted text -

Neat, but I will have a problem if I am dealing with three fields,
right?
--
http://mail.python.org/mailman/listinfo/python-list

Reply via email to