Sort()

sismex01 at hebmex.com sismex01 at hebmex.com
Thu Jun 5 12:42:07 EDT 2003


> From: Turhan Ozen [mailto:txo at cs.nott.ac.uk]
> Sent: Thursday, June 05, 2003 11:32 AM
> 
> I created a class and modified the default _comp_  function.  
> I can sort an array of instances of this class. But I don't
> want to change the original array. Instead, I want to keep
> the positions after sorting in an attribute of the instances.
> 
> Instead of [a1,a2,a3] becoming [a2,a1,a3], I would like the 
> order remain same but
> 
> a1.rank=2
> a2.rank=1
> a3.rank=3
> 
> How can I do this? Does this functionality already exist? If 
> not could you please give me advice on how to implement it?
> I have read the HOWTO on sorting, there is an example of sorting
> classes there. Is there any other place where I can find more
> examples of using Python functions?
> 
> Thanks,
> Turhan.
>

Hmmm... you could simulate it with:

## First we have...
L = [ ... original list ... ]

## Now, we create a sorted version of the list:
SL = L[:]
SL.sort()

## We now create a dictionary keyed by item, relating to it's index:
D = dict( zip(SL, range(len(SL))) )

## 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]



HTH!

-gca
Advertencia:La informacion contenida en este mensaje es confidencial y
restringida, por lo tanto esta destinada unicamente para el uso de la
persona arriba indicada, se le notifica que esta prohibida la difusion de
este mensaje. Si ha recibido este mensaje por error, o si hay problemas en
la transmision, favor de comunicarse con el remitente. Gracias.





More information about the Python-list mailing list