2017-04-04 38 views
1

比方說,我有以下三個列表:迭代追加列在Python數組

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] 

,我想加入他們的矩陣,其中第一列是後面out然後再次calc_points然後inoutinoutout。所以第一列只有一次,而另外兩列重複5次。

我想是這樣的:

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

但像想象中這是行不通的。而不是

calc_point | inout | out | inout |出來....

它產生

calc_point | inout | out

calc_point | inout |出

+0

如果在'/ out'所有的值只增加'points',你甚至不需要串連。只需使用'外部'加法:'points [:,None] + np.array(([0] + [1,3] * 5))' – hpaulj

回答

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], 

(等)

+0

你在5之後有一個額外的方括號:) – kmario23

+0

@ kmario23:哎呀。感謝您指出。 –

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], 
# ... 
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