[Python-talk] reduction Q

Lloyd Kvam python at venix.com
Thu Oct 22 12:30:56 EDT 2009


On Thu, 2009-10-22 at 11:01 -0400, bruce.labitt at autoliv.com wrote:
> There is a generator function that produces as its output 2 arrays, a and 
> b.  I'd like to get the average of the ith elements for both arrays.  The 
> a and b arrays are the same length, if it matters.
> 
> (a,b) = ( myfunc( args ) for f in freqarray )

OK.
> can I do something like the following?
> 
> (suma,sumb) = reduce(add, (a, b))   # this stops the generator chain...
suma,sumb = sum(a),sum(b)


> 
> (avga, avgb) = ( suma, sumb)/freqarray.size

Why not code:

avga = float(sum(a)) / freqarray.size
avgb = float(sum(b)) / freqarray.size

I assume freqarray.size is an integer.  You want to avoid the truncation
you'd get from integer division.
> 
> example: freqarray has 2 elements, then
> 
> a[0,:] = [ 1, 2, 3, ..., 300]                   b[0,:] = [300, 299, 298, 
> ..., 1]
> a[1,:] = [ 0, 1, 2, ..., 299]                   b[1,:] = [299, 298, 297, 
> ..., 0]
> 
> suma = [ 1, 3, 5, ..., 599]                     sumb = [ 599, 597, 595, 
> ..., 1]
> avga =  [ 0.5, 1.5, 2.5, ..., 145.5]    avgb = [ 599/2., 597/2., 595/2., 
> ..., 0.5]
> 
> I don't want arrays a and b to interact.  Is there a way to do this?  I 
> haven't tried this, but it seems like trouble is written all over it :P
> 
> Bruce
> 
> Bruce Labitt
> Autoliv Electronics
> 1011B Pawtucket Blvd, PO Box 1858
> Lowell, MA  01853
> 
> Email: bruce.labitt at autoliv.com. 
> Tel:  (978) 674-6526
> Fax: (978) 674-6581 
> 
> ******************************
> Neither the footer nor anything else in this E-mail is intended to or constitutes an <br>electronic signature and/or legally binding agreement in the absence of an <br>express statement or Autoliv policy and/or procedure to the contrary.<br>This E-mail and any attachments hereto are Autoliv property and may contain legally <br>privileged, confidential and/or proprietary information.<br>The recipient of this E-mail is prohibited from distributing, copying, forwarding or in any way <br>disseminating any material contained within this E-mail without prior written <br>permission from the author. If you receive this E-mail in error, please <br>immediately notify the author and delete this E-mail.  Autoliv disclaims all <br>responsibility and liability for the consequences of any person who fails to <br>abide by the terms herein. <br>
> ******************************
> _______________________________________________
> Python-talk mailing list
> Python-talk at dlslug.org
> http://dlslug.org/mailman/listinfo/python-talk
-- 
Lloyd Kvam
Venix Corp
DLSLUG/GNHLUG library
http://dlslug.org/library.html
http://www.librarything.com/catalog/dlslug
http://www.librarything.com/rsshtml/recent/dlslug
http://www.librarything.com/rss/recent/dlslug



More information about the Python-talk mailing list