Why is this legal?
Michael George Lerner
mlerner at NOumichSPAM.edu
Thu Sep 9 09:54:55 CEST 2004
"Delaney, Timothy C (Timothy)" <tdelaney at avaya.com> wrote:
> Michael George Lerner write:
>> cluster = [(c.resi,c) for c in cluster] # decorate
>> cluster.sort() # sort
>> cluster = [c for (c.resi,c) in cluster] # undecorate
>> That last line actually assigns c.resi for each c in cluster.
> Normally the best way to write the above is:
> cluster = [(c.resi,c) for c in cluster] # decorate
> cluster.sort() # sort
> cluster = [c[-1] for c in cluster] # undecorate
> It's shorter, clearer, works no matter how many elements are in the
> decoration, and doesn't have the danger of your version.
Good points. Thanks.
I'll re-train myself to write it that way.
Heck, maybe I'll even download Python 2.4 so I can say
cluster.sort(key=lambda c: c.resi)
> Tim Delaney
More information about the Python-list