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

Locating index of one list with respect to another list in Python

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

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

[[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]]
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