Python Forum

Full Version: Most efficient way of reshaping a list-array structure
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hi!

I'm dealing with huge data and I'm looking for an efficient way to reshape my list of arrays. Here is my solution which I find to be slow
#x is an input of shape [file,measurement, array(n,r)], i.e.
#len(x) returns file
#len(x[0]) returns measurement
#x[0][0].shape returns (n,r)
the output I require:
#new_x is a list of arrays, its shape is [file*measurement*n, array(r)]
I'm using the following code:

new_x=[]
for ii in range(len(x)):
    for jj in range(len(x[0])):
        for kk in range(len(x[0][0])):
            new_x.append(x[ii][jj][kk])
Is there a more efficient way?
Thank you for your help.
What about
for xi in x:
    for xij in xi:
        new_x.extend(xij)
Much faster, thank you!