数组Python的复杂子集和合并

时间:2013-10-21 16:18:26

标签: python arrays numpy

我有一个9列的numpy数组。我需要能够创建列的子集,然后生成一个只是子集的新数组(例如合并0:3和6:8)。获取单个子集很简单,但我仍然坚持创建一个新数组。

Cmatrix =np.array([ (1,    1,   0,    1,     0,    0,  1, 0,  1),\
                    (1,   -1,   0,    1,     0,    0, -1, 0, -1),\
                    (1,    0,   1,    0,     0,    1,  1, 0,  1),\
                    (1,    0,   1,    0,     0,    1, -1, 0, -1),\
                    (1,    0, 0.5,    0,     0, 0.25,  0, 1,  1),\
                    (1,    0, 0.5,    0,     0, 0.25,  0, 1, -1),\
                    (1,  0.5, 0.5, 0.25,  0.25, 0.25,  1, 0,  1),\
                    (1,  0.5, 0.5, 0.25,  0.25, 0.25,  1, 0,  1),\
                    (1, -0.5, 0.5, 0.25, -0.25, 0.25, -1, 0, -1),\
                    (1, -0.5, 0.5, 0.25, -0.25, 0.25, -1, 0, -1)])

2 个答案:

答案 0 :(得分:3)

np.hstack((Cmatrix[:, :3], Cmatrix[:, 6:8]))

答案 1 :(得分:3)

Cmatrix =np.array([ (1,    1,   0,    1,     0,    0,  1, 0,  1),\
                    (1,   -1,   0,    1,     0,    0, -1, 0, -1),\
                    (1,    0,   1,    0,     0,    1,  1, 0,  1),\
                    (1,    0,   1,    0,     0,    1, -1, 0, -1),\
                    (1,    0, 0.5,    0,     0, 0.25,  0, 1,  1),\
                    (1,    0, 0.5,    0,     0, 0.25,  0, 1, -1),\
                    (1,  0.5, 0.5, 0.25,  0.25, 0.25,  1, 0,  1),\
                    (1,  0.5, 0.5, 0.25,  0.25, 0.25,  1, 0,  1),\
                    (1, -0.5, 0.5, 0.25, -0.25, 0.25, -1, 0, -1),\
                    (1, -0.5, 0.5, 0.25, -0.25, 0.25, -1, 0, -1)]
interesting_cols = range(3) + range(6,8)
new_matrix = Cmatrix[:,interesting_cols]