[Python-talk] reduction Q
Kent Johnson
kent37 at tds.net
Thu Oct 22 15:17:50 EDT 2009
On Thu, Oct 22, 2009 at 2:11 PM, <bruce.labitt at autoliv.com> wrote:
> Well, it turns out there is a simpler way :)
>
> I have tried this, and it even works :) I can show it at PySIG tonight.
>
> def compute_response( f, h ):
> # a few calculations go here...
> retval = [ Ph, Pv, Prec ]
> return retval
>
> for hr in hrlist:
> arrays = (compute_response( freq, hr ) for freq in freqarray )
> (sumh, sumv, sumP) = reduce( add, arrays )
> dBh = 10.0*log10( sumh/ freqarray.size )
> dBv = 10.0*log10( sumv/freqarray.size )
> PdB = 10.0*log10( sumP/freqarray.size )
> # more stuff...
>
> The key was returning retval, rather than [Ph, Pv, Prec]. In this type of
> generator expression, one cannot return the tuple directly.
Um, no. There is absolutely no difference in the value returned between
retval = [ Ph, Pv, Prec ]
return retval
and
return [ Ph, Pv, Prec ]
There is a difference between the above and
return Ph, Pv, Prec
which returns a tuple rather than a list.
I think the big difference from your previous code is that you are not
trying to unpack the generator, and the add function knows what to do
with lists.
Anyway, congrats on getting it working!
Kent
More information about the Python-talk
mailing list