skrat
skrat

Reputation: 748

Iterative appending columns to an array in python

Let's say I have the following three lists:

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]

and I would like to join them in a matrix where the first column is calc_points then inout followed by out then again inout and out. So the the first column is there only once, while the other two repeat 5 times.

I tried like this:

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

But it doesn't work as imagined. Instead of

calc_point | inout | out | inout | out ....

it produces

calc_point | inout | out

calc_point | inout | out

Upvotes: 1

Views: 68

Answers (3)

kmario23
kmario23

Reputation: 61355

Yet another answer without involving loops.

# 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]])

Efficiency: (in descending order)

# 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

Upvotes: 0

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476614

The list comprehension is one level too low. You can however simply use list comprehension inside the subscript:

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

which gives:

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

(and so on)

Upvotes: 2

akuiper
akuiper

Reputation: 214957

Use a list comprehension to construct the columns firstly, then concatenate them:

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

Upvotes: 2

Related Questions