[SciPy-user] Finding Element Position and Sorting

Nathan Bell wnbell at gmail.com
Sat Jan 12 10:52:14 EST 2008


On Jan 12, 2008 9:43 AM, Lorenzo Isella <lorenzo.isella at gmail.com> wrote:
> For instance, the object vector is given by:
> Obj=   [0 1 2 3 4 5 6 7]
> The vector giving the membership of each object to any of the groups 0,1
> or 2 is:
> Mem=[1 1 0 2 2 0 2 1].
> So, 2 objects  in group 0, 3 in group 1 and 3 in group 2.
> Now, I am looking for an efficient way of building a vector getting
> together the identities of the objects in the 1st, 2nd and 3rd group, i.e.:
> Id=[2 5 | 0 1 7 | 3 4 6]
>          0        1        2

Try argsort:

from scipy import *
Obj = array([0, 1, 2, 3, 4, 5, 6, 7])
Mem = array([1, 1, 0, 2, 2, 0, 2, 1])
print argsort(Mem)
print Obj[argsort(Mem)]


If you have multiple keys (Mem in your example), then you can use lexsort()

-- 
Nathan Bell wnbell at gmail.com
http://graphics.cs.uiuc.edu/~wnbell/



More information about the SciPy-User mailing list