How to sort unsort: array(1).sort transform of array(2) -> array(3).unsort (reversed array(1).sort

后端 未结 3 1307
星月不相逢
星月不相逢 2021-01-18 03:50

How do you sort, operate on, and then unsort the result?

Assume I have a float array p1 = 0.15,0.3, 0.25, 0.12, .... It is sorted to: p2 = sort(p1

3条回答
  •  一个人的身影
    2021-01-18 04:46

    You need a double argsort here to keep the order:

    In [6]: a
    Out[6]: array([5, 4, 8, 3, 6, 1, 2, 4, 9, 6])
    
    In [7]: b=sort(a)
    
    In [8]: b
    Out[8]: array([1, 2, 3, 4, 4, 5, 6, 6, 8, 9])
    
    In [9]: ii=a.argsort().argsort()
    
    In [10]: c=b*b
    
    In [11]: c
    Out[11]: array([ 1,  4,  9, 16, 16, 25, 36, 36, 64, 81])
    
    In [12]: c[ii]
    Out[12]: array([25, 16, 64,  9, 36,  1,  4, 16, 81, 36])
    

提交回复
热议问题