How to rearrange array based upon index array

前端 未结 4 602
臣服心动
臣服心动 2020-12-13 17:51

I\'m looking for a one line solution that would help me do the following.

Suppose I have

array = np.array([10, 20, 30, 40, 50])

I\

4条回答
  •  独厮守ぢ
    2020-12-13 18:02

    for those whose index is 2d array, you can use map function. Here is an example:

    a = np.random.randn(3, 3)
    print(a)
    print(np.argsort(a))
    
    print(np.array(list(map(lambda x, y: y[x], np.argsort(a), a))))
    

    the output is

    [[-1.42167035  0.62520498  2.02054623]
     [-0.17966393 -0.01561566  0.24480554]
     [ 1.10568543  0.00298402 -0.71397599]]
    [[0 1 2]
     [0 1 2]
     [2 1 0]]
    [[-1.42167035  0.62520498  2.02054623]
     [-0.17966393 -0.01561566  0.24480554]
     [-0.71397599  0.00298402  1.10568543]]
    

提交回复
热议问题