Re: Is there a better way of doing this?

2009-03-07 Thread Tim Wintle
On Sun, 2009-03-08 at 15:49 +1100, Steven D'Aprano wrote: > If the environmental costs of recycling something are worse than the > environmental costs of throwing it away and making a new one, then > recycling that object is actually harmful. But I digress. Unless you live in a country that import

Re: Is there a better way of doing this?

2009-03-07 Thread Steven D'Aprano
Gabriel Genellina wrote: > Imagine you're working with someone side by side. You write a note in a > piece of paper, put it into an envelope, and hand it to your co-worker. He > opens the envelope, throws it away, takes the note and files it inside a > folder right at the end. And you do this over

Re: Is there a better way of doing this?

2009-03-07 Thread Peter Otten
Lie Ryan wrote: > mattia wrote: >> Yes, sorry, I have to recycle! But how about this: > rw = [[2,4], [4,5,6],[5,5]] > rw += [[1,1]]*2 > rw >> [[2, 4], [4, 5, 6], [5, 5], [1, 1], [1, 1]] >> How can I recicle in this way using append? > > Not .append() but .extend() Whether you use

Re: Is there a better way of doing this?

2009-03-07 Thread Lie Ryan
mattia wrote: Il Sat, 07 Mar 2009 00:05:53 -0200, Gabriel Genellina ha scritto: En Fri, 06 Mar 2009 21:31:01 -0200, mattia escribió: Thanks, I've found another solution here: http://www.obitko.com/tutorials/ genetic-algorithms/selection.php so here is my implementation: def get_fap(fitness

Re: Is there a better way of doing this?

2009-03-07 Thread mattia
Il Sat, 07 Mar 2009 00:05:53 -0200, Gabriel Genellina ha scritto: > En Fri, 06 Mar 2009 21:31:01 -0200, mattia escribió: > >> Thanks, I've found another solution here: >> http://www.obitko.com/tutorials/ >> genetic-algorithms/selection.php >> so here is my implementation: >> >> >> def get_fap(fi

Re: Is there a better way of doing this?

2009-03-06 Thread Paul Rubin
"Gabriel Genellina" writes: > > for x in population: > > f = fitness(x) > > fap += [(f, x)] > > total += f > > return sorted(fap, reverse=True), total > ... > Environmentally friendly Pythoneers avoid using discardable > intermediate envelopes: > > fap.a

Re: Is there a better way of doing this?

2009-03-06 Thread Gabriel Genellina
En Fri, 06 Mar 2009 21:31:01 -0200, mattia escribió: Thanks, I've found another solution here: http://www.obitko.com/tutorials/ genetic-algorithms/selection.php so here is my implementation: def get_fap(fitness, population): fap = [] total = 0 for x in population: f = fi

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 14:13:47 -0800, Scott David Daniels ha scritto: > mattia wrote: >> Here is my last shot, where I get rid of all the old intermediate >> functions: >> >> def selection(fitness, population): >> lp = len(population) >> roulette_wheel = [] >> for x in population: >>

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 18:46:44 -0300, andrew cooke ha scritto: > i have not been following this discussion in detail, so someone may have > already explained this, but it should not be necessary to actually > construct the roulette wheel to select values from it. what you are > doing is selecting f

Re: Is there a better way of doing this?

2009-03-06 Thread Scott David Daniels
mattia wrote: Here is my last shot, where I get rid of all the old intermediate functions: def selection(fitness, population): lp = len(population) roulette_wheel = [] for x in population: roulette_wheel += [x]*fitness(x) selected_population = [[]]*lp selected_popula

Re: Is there a better way of doing this?

2009-03-06 Thread andrew cooke
i have not been following this discussion in detail, so someone may have already explained this, but it should not be necessary to actually construct the roulette wheel to select values from it. what you are doing is selecting from a list where the there are different probabilities of selecting d

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 22:28:00 +0100, Peter Otten ha scritto: > mattia wrote: > >> Il Fri, 06 Mar 2009 14:06:14 +0100, Peter Otten ha scritto: >> >>> mattia wrote: >>> Hi, I'm new to python, and as the title says, can I improve this snippet (readability, speed, tricks): def g

Re: Is there a better way of doing this?

2009-03-06 Thread Peter Otten
mattia wrote: > Il Fri, 06 Mar 2009 14:06:14 +0100, Peter Otten ha scritto: > >> mattia wrote: >> >>> Hi, I'm new to python, and as the title says, can I improve this >>> snippet (readability, speed, tricks): >>> >>> def get_fitness_and_population(fitness, population): >>> return [(fitness(

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 14:06:14 +0100, Peter Otten ha scritto: > mattia wrote: > >> Hi, I'm new to python, and as the title says, can I improve this >> snippet (readability, speed, tricks): >> >> def get_fitness_and_population(fitness, population): >> return [(fitness(x), x) for x in population

Re: Is there a better way of doing this?

2009-03-06 Thread Paul Rubin
Chris Rebert writes: >for i in range(len(fap)): >selected_population.append(choice(rw)) "for i in range(len(something))" is a bit of a code smell. You could instead say: selected_population.extend(choice(rw) for x in fap) The unused "x" is also a slight code smell, but the most

Re: Is there a better way of doing this?

2009-03-06 Thread Peter Otten
mattia wrote: > Hi, I'm new to python, and as the title says, can I improve this snippet > (readability, speed, tricks): > > def get_fitness_and_population(fitness, population): > return [(fitness(x), x) for x in population] > > def selection(fitness, population): > ''' > Select the

Re: Is there a better way of doing this?

2009-03-06 Thread Chris Rebert
On Fri, Mar 6, 2009 at 3:52 AM, mattia wrote: > Il Fri, 06 Mar 2009 03:43:22 -0800, Chris Rebert ha scritto: > >> On Fri, Mar 6, 2009 at 3:07 AM, mattia wrote: >>> Great, the for statement has not to deal with fap anymore, but with >>> another sequence, like this: >>> >>> def get_roulette_wheel(w

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 03:43:22 -0800, Chris Rebert ha scritto: > On Fri, Mar 6, 2009 at 3:07 AM, mattia wrote: >> Great, the for statement has not to deal with fap anymore, but with >> another sequence, like this: >> >> def get_roulette_wheel(weight_value_pairs): >>    roulette_wheel = [] >>    for

Re: Is there a better way of doing this?

2009-03-06 Thread Chris Rebert
On Fri, Mar 6, 2009 at 3:07 AM, mattia wrote: > Great, the for statement has not to deal with fap anymore, but with > another sequence, like this: > > def get_roulette_wheel(weight_value_pairs): >    roulette_wheel = [] >    for weight, value in weight_value_pairs: >        roulette_wheel += [valu

Re: Is there a better way of doing this?

2009-03-06 Thread mattia
Il Fri, 06 Mar 2009 10:19:22 +, mattia ha scritto: > Hi, I'm new to python, and as the title says, can I improve this snippet > (readability, speed, tricks): > > def get_fitness_and_population(fitness, population): > return [(fitness(x), x) for x in population] > > def selection(fitness,

Re: Is there a better way of doing this?

2009-03-06 Thread Chris Rebert
On Fri, Mar 6, 2009 at 2:19 AM, mattia wrote: > Hi, I'm new to python, and as the title says, can I improve this snippet > (readability, speed, tricks): > > def get_fitness_and_population(fitness, population): >    return [(fitness(x), x) for x in population] > > def selection(fitness, population)

Is there a better way of doing this?

2009-03-06 Thread mattia
Hi, I'm new to python, and as the title says, can I improve this snippet (readability, speed, tricks): def get_fitness_and_population(fitness, population): return [(fitness(x), x) for x in population] def selection(fitness, population): ''' Select the parent chromosomes from a popula

Re: Is there a better way of doing this?

2005-06-02 Thread Steven D'Aprano
On Mon, 30 May 2005 14:05:36 +0200, Magnus Lycka wrote: > Steven D'Aprano wrote: > > print is a statement, not a function. The brackets are syntactically > > correct, but pointless. Remove them. > ... >> On Sat, 28 May 2005 13:24:19 +, Michael wrote: >>> while( newNS ): >> >> Guido (our Ben

Re: Is there a better way of doing this?

2005-05-30 Thread Magnus Lycka
Steven D'Aprano wrote: > print is a statement, not a function. The brackets are syntactically > correct, but pointless. Remove them. ... > On Sat, 28 May 2005 13:24:19 +, Michael wrote: >> while( newNS ): > > Guido (our Benevolent Dictator For Life and creator of Python) hates > seeing white

Re: Is there a better way of doing this?

2005-05-29 Thread Duncan Booth
Michael wrote: > I'm fairly new at Python, and have the following code that works but > isn't very concise, is there a better way of writing it?? It seems > much more lengthy than python code i have read. :-) > (takes a C++ block and extracts the namespaces from it) Yes, there is a better way

Re: Is there a better way of doing this?

2005-05-29 Thread Roman Yakovenko
On 5/29/05, Cyril BAZIN <[EMAIL PROTECTED]> wrote: > Hi, > > I don't know very well what you want to do, but if you want to parse c++, > take a look at "GCC-XML python" (http://www.gccxml.org) and the python > binding (http://pygccxml.sourceforge.net/). These tools > translate c++ code to XML. T

Re: Is there a better way of doing this?

2005-05-29 Thread Cyril BAZIN
Hi, I don't know very well what you want to do, but if you want to parse c++, take a look at "GCC-XML python" (http://www.gccxml.org) and the python binding (http://pygccxml.sourceforge.net/). These tools translate c++ code to XML. Then, you can parse xml with your favorite tools and find the name

Re: Is there a better way of doing this?

2005-05-29 Thread Fredrik Lundh
Steven D'Aprano wrote: > Guido (our Benevolent Dictator For Life and creator of Python) hates > seeing whitespace next to parentheses. I agree with him. while(newNS) > good, while( newNS ) bad. while is a statement, so while(newNS) is bad in more than one way. -- http://mail.python.org/mail

Re: Is there a better way of doing this?

2005-05-29 Thread Steven D'Aprano
On Sat, 28 May 2005 13:24:19 +, Michael wrote: > Hi, > I'm fairly new at Python, and have the following code that works but isn't > very concise, is there a better way of writing it?? It seems much more > lengthy than python code i have read. :-) > (takes a C++ block and extracts the names

Is there a better way of doing this?

2005-05-28 Thread Michael
Hi, I'm fairly new at Python, and have the following code that works but isn't very concise, is there a better way of writing it?? It seems much more lengthy than python code i have read. :-) (takes a C++ block and extracts the namespaces from it) def ExtractNamespaces(data): print("Extract