Sort()

Bengt Richter bokr at oz.net
Thu Jun 5 16:16:10 EDT 2003


On 5 Jun 2003 18:26:08 GMT, bokr at oz.net (Bengt Richter) wrote:

>On Thu, 5 Jun 2003 11:42:07 -0500, sismex01 at hebmex.com wrote:
>
[...]
>>
>>## And now, we transfer the index position as an attribute to
>>## the items on the original list:
>>for item in L:
>>   item.rank = D[item]
>>
>Might be a problem if some objects are not hashable, or if they
>hash to identical values (i.e., duplicates in list), so maybe
>
>for i in xrange(len(SL)): setattr(SL[i], 'rank', i)
>
>is safer. E.g.,
>
[...]
Why did I use setattr? I don't know. My mind was meandering ;-/

    for i in xrange(len(SL)): SL[i].rank = i

is clearer, as in Steven Taschuk's example. And he wrapped it nicely
in a function too ;-)

Regards,
Bengt Richter




More information about the Python-list mailing list