Python Forum
Most efficient way of reshaping a list-array structure
Thread Rating:
  • 1 Vote(s) - 5 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Most efficient way of reshaping a list-array structure
#1
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.
Reply
#2
What about
for xi in x:
    for xij in xi:
        new_x.extend(xij)
Reply
#3
Much faster, thank you!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  A more efficient code titanif 2 459 Oct-17-2023, 02:07 PM
Last Post: deanhystad
  Cleaning my code to make it more efficient BSDevo 13 1,275 Sep-27-2023, 10:39 PM
Last Post: BSDevo
  Making a function more efficient CatorCanulis 9 1,750 Oct-06-2022, 07:47 AM
Last Post: DPaul
  functional LEDs in an array or list? // RPi user Doczu 5 1,521 Aug-23-2022, 05:37 PM
Last Post: Yoriz
  Reshaping a single column in to multiple column using Python sahar 7 1,968 Jun-20-2022, 12:35 PM
Last Post: deanhystad
  LIST or ARRAY Comparison and change of value nio74maz 0 1,669 Dec-21-2020, 05:52 PM
Last Post: nio74maz
  I there a more efficient way of printing ? Capitaine_Flam 7 3,414 Dec-01-2020, 10:37 AM
Last Post: buran
  2d Array adds last element to entire list waiteup 2 2,025 Nov-19-2020, 08:25 PM
Last Post: bowlofred
  trouble with list array Milfredo 2 1,989 Sep-16-2020, 12:07 AM
Last Post: Milfredo
  Finding an element in a 1d list in a 2d array lionrocker221 0 1,787 Jun-27-2020, 04:50 PM
Last Post: lionrocker221

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020