在numpy下合并两个数组

Rao*_*oul 3 python arrays numpy

使用Numpy,我想得到下面的结果,给出b和c.我已经研究过堆叠功能,但我无法让它工作.有人可以帮忙吗?

import numpy as np

a=range(35,135)

b=np.reshape(a,(10,10))
c=np.array([[5,5],[5,6],[5,7],[6,5],[6,6],[6,7],[7,5],[7,6],[7,7]])
Run Code Online (Sandbox Code Playgroud)

结果应如下所示:

np.array([[5,5,90],[5,6,91],[5,7,92],[6,5,100],[6,6,101],[6,7,102],
          [7,5,110],[7,6,111],[7,7,112]])
Run Code Online (Sandbox Code Playgroud)

mgi*_*son 8

唷!这是一个doosie.首先,我们使用numpy的花式索引来提取你想要的项目:

>>> b[tuple(c.T)]
array([ 90,  91,  92, 100, 101, 102, 110, 111, 112])
Run Code Online (Sandbox Code Playgroud)

然后,唯一剩下的就是将该数组堆叠起来c使用column_stack:

>>> np.column_stack((c,b[tuple(c.T)]))
array([[  5,   5,  90],
       [  5,   6,  91],
       [  5,   7,  92],
       [  6,   5, 100],
       [  6,   6, 101],
       [  6,   7, 102],
       [  7,   5, 110],
       [  7,   6, 111],
       [  7,   7, 112]])
Run Code Online (Sandbox Code Playgroud)

  • 索引技巧c_也会这样:`np.c_ [c,b [tuple(cT)]]` (2认同)