1

I have an list of integers:

 x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 
      32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]

And second list that can only contain elements from x, for example: y = [44, 11, 49]

I need to find the index of each element of y in x.

cglacet
  • 6,638
  • 2
  • 35
  • 49
SilverCrow
  • 147
  • 1
  • 10

2 Answers2

1

.index() is what you're after.

x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]
y = [44, 11, 49]
for a in y:
    print(x.index(a))

EDIT: Provided that each element of y appears once, and only once in x.

JimmyCarlos
  • 1,901
  • 1
  • 10
  • 23
1
x = [0, 1, 3, 5, 6, 7, 33, 39, 49, 51, 11, 32, 55, 61, 31, 44, 43, 4, 45, 30, 50, 41]
y = [44, 11, 49]
indexs=[x.index(i) for i in y if i in x]
print(indexs)

Or

ind=list(map(x.index,y))
print(ind)
MrNobody33
  • 6,143
  • 5
  • 18