I have two arrays, A and B. Based on a function, I create a subset C from A and a subset D from B. Now, I want to join them together ( I want to keep those items that are in C and D with the same index in A and B).
A = np.array([1,2,3,4,5,6,7,8,9,10,11,12,13,14,15,16,17,18,19,20])
B = np.array([20,19,18,17,16,15,14,13,12,11,10,9,8,7,6,5,4,3,2,1])
C = np.array([3,7,13])
D = np.array([18,8])
fvl1 = list()
fvl2 = list()
for i in C:
index = np.where(A == i)
if B[index] in D:
fvl1.append(A[index])
fvl2.append(B[index])
print(fvl1)
print(fvl2)
Output:
[array([3]), array([13])]
[array([18]), array([8])]
This is what I've done until now, but it's really slow. Is there any way to optimize this for it to go faster?
LE: You can see that the element 3 in A has the same index as element 18 in B(index = 3), same with element 13 from A and 8 from B.
Aalways sorted?