Issue
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])
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])
Answered By - Nin17
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.