Say two lists, X and Y are given in Python. Y contains X, but the elements in X may occur multiple times in Y and Y may contain other elements as well. I want to make lists that contain the positions of the elements in X in Y (each element apart). here is an example:
given X= [1, 8, 5, 6] and Y= [10, 5, 1, 8, 1, 8, 5, 1, 8, 6]
output :
- [2, 4, 7] for "1"
- [3, 5, 8] for "8"
- [1, 6] for "5"
- [9] for "6"
Here is my attempt:
x = 0
Positions_x = []
while x <= len(X):
for pos, char in enumerate(Y):
if char == X[x]:
Positions_x.append(pos)
x += 1
print(Positions_x)
And here is what i get as output:
[2, 4, 7]
[2, 4, 7, 3, 5, 8]
[2, 4, 7, 3, 5, 8, 1, 6]
[2, 4, 7, 3, 5, 8, 1, 6, 9]
Can anyone help me get the desired output?