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

Deleting some elements and flattening the array in Python

I have an array, R. I would like to remove elements corresponding to indices in Remove and then flatten with the remaining elements. The desired output is attached.

R=np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
       [6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
       [1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])

Remove = [(0, 1),(0,2)] 

R1 = R.flatten()
print([R1])

The desired output is

array([1.05567452e+11, 6.94076420e+09, 1.96129124e+10, 1.11642674e+09,
       1.88618492e+10, 1.73640817e+10, 4.84980874e+09])

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 do this with list comprehension:

import numpy as np
R=np.array([[1.05567452e+11, 1.51583103e+11, 5.66466172e+08],
       [6.94076420e+09, 1.96129124e+10, 1.11642674e+09],
       [1.88618492e+10, 1.73640817e+10, 4.84980874e+09]])

Remove = [(0, 1),(0,2)] 
b = [[j for i, j in enumerate(m) if (k, i) not in Remove] for k, m in enumerate(R)]
R1 = np.array([i for j in b for i in j]) #Flatten the resulting list

print(R1)

Output

array([1.05567452e+11, 6.94076420e+09, 1.96129124e+10, 1.11642674e+09,
       1.88618492e+10, 1.73640817e+10, 4.84980874e+09])
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