简体   繁体   中英

Convert a list of numpy arrays to a 5D numpy array

I am having a database of 7000 objects (list_of_objects), each one of these files contains a numpy array with size of 10x5x50x50x3 . I would like to create a 5d numpy array that will contain 7000*10x5x50x50x3 . I tried to do so using two for-loops. My sample code:

fnl_lst = []
for object in list_of_objects:
     my_array = read_array(object) # size 10x5x50x50x3
     for ind in my_array:
        fnl_lst.append(ind)
fnl_lst= np.asarray( fnl_lst) # print(fnl_lst) -> (70000,)

That code result in the end in a nested numpy array which contains 70000 arrays each of them has a size of 5x50x50x3 . However, I would like instead to build a 5d array with size 70000x5x50x50x3 . How can I do that instead?

fnl_lst = np.stack([ind for ind in read_array(obj) for obj in list_of_objects])

or, just append to the existing code:

fnl_lst = np.stack(fnl_lst)

UPD: by hpaulj's comment, if my_array is indeed 10x5x50x50x3, this might be enough:

fnl_lst = np.stack([read_array(obj) for obj in list_of_objects])

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