Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Reshape each array in a list of arrays python

I have an list of arrays, I want to reshape each array in the list and then stack.

A sample below

C = np.array([[-127, -108, -290],
       [-123,  -83, -333],
       [-126,  -69, -354],
       [-146, -211, -241],
       [-151, -209, -253],
       [-157, -200, -254]])



D = np.array([[-129, -146, -231],
       [-127, -148, -238],
       [-132, -157, -231],
       [ -93, -355, -112],
       [ -95, -325, -137],
       [ -99, -282, -163]])



E = np.array(([[-141, -133, -200],
       [-132, -123, -202],
       [-119, -117, -204],
       [-107, -210, -228],
       [-101, -194, -243],
       [-105, -175, -244]]))


ArrayList = (C,D,E)

to reshape an individual array I do the following

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

newArray = ArrayList[0].reshape(1,-1)


and produces the desired result

array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
        -241, -151, -209, -253, -157, -200, -254]])

I tried writing a for loop to go through each item

newArray = []
for i in ArrayList:
    i.reshape(1,-1)
    newArray.append(i)
    

But I got the same product as what I started with.
The desired output is shown below

(array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
         -241, -151, -209, -253, -157, -200, -254]]),
 array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
         -112,  -95, -325, -137,  -99, -282, -163]]),
 array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
         -228, -101, -194, -243, -105, -175, -244]]))

Any help appreciated.

>Solution :

I think you just need to do this:

newArray = []
for i in ArrayList:
    j = i.reshape(1,-1)
    newArray.append(j)

print(newArray)

Output:

[array([[-127, -108, -290, -123,  -83, -333, -126,  -69, -354, -146, -211,
        -241, -151, -209, -253, -157, -200, -254]]), 
array([[-129, -146, -231, -127, -148, -238, -132, -157, -231,  -93, -355,
        -112,  -95, -325, -137,  -99, -282, -163]]), 
array([[-141, -133, -200, -132, -123, -202, -119, -117, -204, -107, -210,
        -228, -101, -194, -243, -105, -175, -244]])]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading