Is there a better way of doing this?

Paul Rubin http
Fri Mar 6 21:14:44 EST 2009


"Gabriel Genellina" <gagsl-py2 at yahoo.com.ar> 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.append((f, x))

I'd probably use:

     fap = list((fitness(x),x) for x in population)
     total = sum(x for x,y in fap)
     return sorted(fap, reverse=True), total



More information about the Python-list mailing list