[Python-talk] Generator Question
Lloyd Kvam
lkvam at venix.com
Fri Aug 14 17:53:43 EDT 2009
On Fri, 2009-08-14 at 17:19 -0400, bruce.labitt at autoliv.com wrote:
> kent3737 at gmail.com wrote on 08/13/2009 12:42:04 PM:
>
> > On Thu, Aug 13, 2009 at 11:58 AM, <bruce.labitt at autoliv.com> wrote:
> > > Will the group of generators cascade the
> > > same?
> >
> > Yes, all that is changing is the source of the first generator (from a
> > generator function instead of a generator expression).
> >
> > > Say we use the previous example. Something like this, or would I need
> to
> > > do something different?
> > >
> > > seeds = [ <some sequence that defines the arrays to be created> ]
> > > score = 0
> > > while score < threshold:
> > > #arrays = (make_array(seed) for seed in seeds)
> > > arrays = make_array(seed) # instead of line above
> > > fft_results = (compute_fft(array) for array in arrays)
> > > powers = (compute_power(result) for result in fft_results)
> > > score, seeds = optimize(powers)
> >
> > Right.
> >
> > > This is really powerful stuff...
> >
> > Yes :-)
> >
> > Kent
>
> Question about the last stage, what I would call the consumer of the
> generator data.
>
> I have written make_array, compute_fft, and compute_power. Instead of
> using the
> routine optimize (which I have not written yet), I want to test what I
> have.
>
> I am trying to write a function called max_power.
>
> The output of the function is the element by element column maximum of the
> arrays in powers.
>
> Consider the following as a matrix
>
matrix = [ # added commas
> [ 0, 1, 2, 3, 4],
> [ 5, 6, 7, 8, 9],
> [10, 0, 1, 9, 7],
] # 2 dim matrix as list of lists
>
> 10 6 7 9 9 <== column max
# the transpose function is zip( *)
# find the max of each col
# collect the maxs with a list comprehension
In [6]: maxs = [max(col) for col in zip(*matrix)]
In [7]: maxs
Out[7]: [10, 6, 7, 9, 9]
>
> There is a numpy function called maximum
> It returns an array c whose ith element is the maximum of (ai, bi)
>
> c = maximum( a, b ), ci = max(ai, bi) for i in c
maxes = max(col for col
>
> The output of max_power should be one array that is the column maximum of
> the matrix
>
> [ powers row 0 ] first iteration of generator
> [ powers row 1 ] 2nd
> .
> .
> .
> [ powers row last ] last
>
>
> So I want max_power to consume all of the data from the previous stages
> and only return
> a single row which is the column by column maximum.
>
> Now there really isn't a real matrix here, just a series of arrays (or
> rows) that are output
> by the previous generator.
list( generator output) will collect the outputs into a list
I presume
list( powers)
will create the matrix as a list of list
maxs = [ max(col) for col in zip( *list( powers)) ]
I hope that's close to what you're looking for.
>
> I feel like I am really close here, but this conceptual part is holding me
> back.
>
> Any suggestions?
>
> Thanks!
>
> Bruce
>
>
> ******************************
> 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.
1 Court Street, Suite 378
Lebanon, NH 03766-1358
voice: 603-653-8139
fax: 320-210-3409
More information about the Python-talk
mailing list