简体   繁体   中英

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.

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

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)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM