迭代将列附加到python中的数组

时间:2017-04-04 17:04:15

标签: python arrays numpy matrix

假设我有以下三个列表:

calc_points=np.asarray(
       [ 0,  1,  2,  3,  4,  5,  6,  7,  8,  9, 10, 11, 12, 13, 14, 15, 16,
       17, 18, 19, 21, 23, 25, 27, 29, 31, 33, 35, 37, 39, 41, 43, 45, 47,
       49, 51, 53, 55, 57, 59, 61, 63, 65, 67, 69, 71, 73, 75, 77, 79, 81,
       83, 85, 87, 89, 91, 93, 95, 97, 99])
out=[c+1 for c in calc_points]
inout=[c+3 for c in calc_points]

我希望将它们加入矩阵,其中第一列为calc_points,然后是inout,然后是out,然后是inoutout。所以第一列只有一次,而其他两次重复5次。

我试过这样:

temp=[np.c_[calc_points,inout,out] for i in range(5)]

但它没有像想象的那样起作用。而不是

  

calc_point | inout |出| inout |出来......

它产生

  

calc_point | inout |出

     

calc_point | inout |出

3 个答案:

答案 0 :(得分:2)

首先使用列表推导来构造列,然后将它们连接起来:

np.stack([calc_points]+[col for _ in range(5) for col in [calc_points+3, calc_points+1]], axis=-1)

#array([[  0,   3,   1,   3,   1,   3,   1,   3,   1,   3,   1],
#       [  1,   4,   2,   4,   2,   4,   2,   4,   2,   4,   2],
#       [  2,   5,   3,   5,   3,   5,   3,   5,   3,   5,   3],
#       [  3,   6,   4,   6,   4,   6,   4,   6,   4,   6,   4],
#       [  4,   7,   5,   7,   5,   7,   5,   7,   5,   7,   5],
#       [  5,   8,   6,   8,   6,   8,   6,   8,   6,   8,   6],
# ...

答案 1 :(得分:2)

列表理解度太低了。但是,你可以简单地使用列表理解里面下标:

np.c_[(calc_points,)+(inout,out)*5]

给出:

>>> np.c_[(calc_points,)+(inout,out)*5]
array([[  0,   3,   1,   3,   1,   3,   1,   3,   1,   3,   1],
       [  1,   4,   2,   4,   2,   4,   2,   4,   2,   4,   2],
       [  2,   5,   3,   5,   3,   5,   3,   5,   3,   5,   3],
       [  3,   6,   4,   6,   4,   6,   4,   6,   4,   6,   4],
       [  4,   7,   5,   7,   5,   7,   5,   7,   5,   7,   5],
       [  5,   8,   6,   8,   6,   8,   6,   8,   6,   8,   6],
       [  6,   9,   7,   9,   7,   9,   7,   9,   7,   9,   7],
       [  7,  10,   8,  10,   8,  10,   8,  10,   8,  10,   8],
       [  8,  11,   9,  11,   9,  11,   9,  11,   9,  11,   9],
       [  9,  12,  10,  12,  10,  12,  10,  12,  10,  12,  10],
       [ 10,  13,  11,  13,  11,  13,  11,  13,  11,  13,  11],
       [ 11,  14,  12,  14,  12,  14,  12,  14,  12,  14,  12],
       [ 12,  15,  13,  15,  13,  15,  13,  15,  13,  15,  13],
       [ 13,  16,  14,  16,  14,  16,  14,  16,  14,  16,  14],

(依此类推)

答案 2 :(得分:0)

另一个没有涉及循环的答案。

# make calc_points a column vector
In [49]: calc_points[:, np.newaxis]

# make array from the list repetitions
In [50]: np.array((inout, out) * 5).T

# concatenate all of them using np.hstack
In [51]: np.hstack((calc_points[:, np.newaxis], np.array([inout, out] * 5).T))
Out[51]: 
array([[  0,   3,   1,   3,   1,   3,   1,   3,   1,   3,   1],
       [  1,   4,   2,   4,   2,   4,   2,   4,   2,   4,   2],
       [  2,   5,   3,   5,   3,   5,   3,   5,   3,   5,   3],
       [  3,   6,   4,   6,   4,   6,   4,   6,   4,   6,   4],
       [  4,   7,   5,   7,   5,   7,   5,   7,   5,   7,   5],
       [  5,   8,   6,   8,   6,   8,   6,   8,   6,   8,   6],
       [  6,   9,   7,   9,   7,   9,   7,   9,   7,   9,   7],
       [  7,  10,   8,  10,   8,  10,   8,  10,   8,  10,   8],
       [  8,  11,   9,  11,   9,  11,   9,  11,   9,  11,   9],
       [  9,  12,  10,  12,  10,  12,  10,  12,  10,  12,  10],
       [ 10,  13,  11,  13,  11,  13,  11,  13,  11,  13,  11],
       .....
       .....
       [ 99, 102, 100, 102, 100, 102, 100, 102, 100, 102, 100]])

效率:(按降序排列)

# interestingly list comprehension seems to be running like a war horse.
In [52]: %timeit np.stack([calc_points]+[col for _ in range(5) for col in [calc_points+3, calc_points+1]], axis=-1)
10000 loops, best of 3: 81.9 µs per loop

# almost 5x faster than using `np.c_`
In [53]: %timeit np.hstack((calc_points[:, np.newaxis], np.array((inout, out) * 5).T))
10000 loops, best of 3: 98.6 µs per loop

In [54]: %timeit np.c_[(calc_points,)+(inout,out)*5]
1000 loops, best of 3: 458 µs per loop