Python Forum
Most efficient way of reshaping a list-array structure - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: Most efficient way of reshaping a list-array structure (/thread-21332.html)



Most efficient way of reshaping a list-array structure - midarq - Sep-25-2019

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.


RE: Most efficient way of reshaping a list-array structure - Gribouillis - Sep-25-2019

What about
for xi in x:
    for xij in xi:
        new_x.extend(xij)



RE: Most efficient way of reshaping a list-array structure - midarq - Sep-25-2019

Much faster, thank you!