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

Extracting elements from an array in Python

I have an array that looks as follows:

array([
      [[[0.08467145],
         [0.0846905 ]]],
       [[[0.08470057],
         [0.08483638]]],


       [[[0.0846846 ],
         [0.08471105]]],


       [[[0.08469571],
         [0.08472978]]]], dtype=float32)

I want to extract the first element from each pair and store in a list and also extract the second element and store it in another list. How can I do this?

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

>Solution :

You can use array indexing with np.ndarray.flatten:

print(a[:,:,0].flatten())
print(a[:,:,1].flatten())

This outputs:

[0.08467145 0.08470057 0.0846846  0.08469571]
[0.0846905  0.08483638 0.08471105 0.08472978]
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