Is there a better way of doing this?

Chris Rebert clp2 at rebertia.com
Fri Mar 6 12:43:22 CET 2009


On Fri, Mar 6, 2009 at 3:07 AM, mattia <gervaz at gmail.com> 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 += [value]*weight
>    return roulette_wheel
>
> def selection(fitness, population):
>    ...
>    rw = get_roulette_wheel(fap)
>    for i in range(pop_len-2):
>        selected_population += [choice(rw)]
>    return selected_population
>
> I think that using [choice(rw)]*len(fap) will produce the same sequence
> repeted len(fap) times...

Revision to this new code:

def get_roulette_wheel(weight_value_pairs):
   return [[value]*weight for weight, value in weight_value_pairs]

def selection(fitness, population):
   ...
   rw = get_roulette_wheel(fap)
   for i in range(len(fap)):
       selected_population.append(choice(rw))
   return selected_population

Cheers,
Chris

-- 
I have a blog:
http://blog.rebertia.com



More information about the Python-list mailing list