mneuner
mneuner

Reputation: 447

2D python list of numpy arrays to 2D numpy array

Which is the most performant way to convert something like that

problem = [ [np.array([1,2,3]), np.array([4,5])],
            [np.array([6,7,8]), np.array([9,10])]]

into

desired = np.array([[1,2,3,4,5], 
                   [6,7,8,9,10]])

Unfortunately, the final number of columns and rows (and length of subarrays) is not known in advance, as the subarrays are read from a binary file, record by record.

Upvotes: 1

Views: 998

Answers (2)

kezzos
kezzos

Reputation: 3221

I think this:

print np.array([np.hstack(i) for i in problem])

Using your example, this runs in 0.00022s, wherease concatenate takes 0.00038s

You can also use apply_along_axis although this runs in 0.00024s:

print np.apply_along_axis(np.hstack, 1, problem)

Upvotes: 2

Carles Mitjans
Carles Mitjans

Reputation: 4866

How about this:

problem = [[np.array([1,2,3]), np.array([4,5])],
        [np.array([6,7,8]), np.array([9,10])]]

print np.array([np.concatenate(x) for x in problem])

Upvotes: 5

Related Questions