I have lists J1 and J2. I want to locate the index of elements of J2 with respect to J1. For example, J2[0]=2 and this occurs at J1[1]. Hence, the index should be 1. Similarly for other elements of J2. I present the current and expected outputs.
import numpy as np
J1=[[1, 2, 4, 6, 7, 9, 10]]
J2=[[2, 6, 7, 9, 10]]
Indices=[i for i in J2 and J1]
print(Indices)
The current output is
[[1, 2, 4, 6, 7, 9, 10]]
The expected output is
[[1,3,4,5,6]]
>Solution :
You can use index() to find the position of an item in a list.
import numpy as np
J1=[[1, 2, 4, 6, 7, 9, 10]]
J2=[[2, 6, 7, 9, 10]]
Indices=[[J1[0].index(i) for i in J2[0]]]
print(Indices)
Output:
[[1, 3, 4, 5, 6]]