从其他numpy数组创建numpy数组

时间:2014-04-10 19:11:45

标签: python arrays numpy row

我有这个数组:

data = [[  1.00000000e-01   1.00000000e-03]
 [  2.00000000e-01   2.00000000e-03]
 [  3.00000000e-01   3.00000000e-03]
 [  4.00000000e-01   4.00000000e-03]
 [  5.00000000e-01   5.00000000e-03]
 [  6.00000000e-01   6.00000000e-03]
 [  7.00000000e-01   7.00000000e-03]
 [  8.00000000e-01   8.00000000e-03]
 [  9.00000000e-01   9.00000000e-03]
 [  1.00000000e+00   1.00000000e-02]
 [  1.10000000e+00   1.10000000e-02]
 [  1.20000000e+00   1.20000000e-02]
 [  1.30000000e+00   1.30000000e-02]
 [  1.40000000e+00   5.23359560e-02]
 [  1.50000000e+00   1.04528463e-01]
 [  1.60000000e+00   1.56434465e-01]
 [  1.70000000e+00   2.07911691e-01]
 [  1.80000000e+00   2.58819045e-01]
 [  1.90000000e+00   3.09016994e-01]
 [  2.00000000e+00   3.58367950e-01]
 [  2.10000000e+00   4.06736643e-01]
 [  2.20000000e+00   4.53990500e-01]
 [  2.30000000e+00   5.00000000e-01]
 [  2.40000000e+00   5.44639035e-01]
 [  2.50000000e+00   5.87785252e-01]
 [  2.60000000e+00   6.29320391e-01]
 [  2.70000000e+00   6.69130606e-01]
 [  2.80000000e+00   7.07106781e-01]
 [  2.90000000e+00   7.43144825e-01]
 [  3.00000000e+00   7.77145961e-01]
 [  3.10000000e+00   8.09016994e-01]
 [  3.20000000e+00   8.38670568e-01]
 [  3.30000000e+00   8.66025404e-01]
 [  3.40000000e+00   8.91006524e-01]
 [  3.50000000e+00   9.13545458e-01]
 [  3.60000000e+00   9.33580426e-01]
 [  3.70000000e+00   9.51056516e-01]
 [  3.80000000e+00   9.65925826e-01]
 [  3.90000000e+00   9.78147601e-01]
 [  4.00000000e+00   9.87688341e-01]
 [  4.10000000e+00   9.94521895e-01]
 [  4.20000000e+00   9.98629535e-01]
 [  4.30000000e+00   1.00000000e+00]
 [  4.40000000e+00   1.00100000e+00]
 [  4.50000000e+00   1.00200000e+00]
 [  4.60000000e+00   1.00300000e+00]
 [  4.70000000e+00   1.00400000e+00]
 [  4.80000000e+00   1.00500000e+00]
 [  4.90000000e+00   1.00600000e+00]
 [  5.00000000e+00   1.00700000e+00]]

我需要从中创建第二个数组,该数组由每行的第一个值组成:

rows = [[1.00000000e-01], [2.00000000e-01], ...., [5.00000000e+00]]

有一条单行方式吗?感谢

1 个答案:

答案 0 :(得分:1)

设置样本数组:

In [17]: a = np.arange(10).reshape(5,2)

In [18]: a
Out[18]:
array([[0, 1],
       [2, 3],
       [4, 5],
       [6, 7],
       [8, 9]])

然后取决于每行中第一个值产生的数组形状:

In [19]: a[:,0]
Out[19]: array([0, 2, 4, 6, 8])

In [20]: a[:,0][:,None] # or a[:,0,None] or a[:,0,np.newaxis]
Out[20]:
array([[0],
       [2],
       [4],
       [6],
       [8]])