1

I earlier asked this question and the following was suggested: How to test if a list contains another list? However, the above did not answer my question.

listA=["mango","orange","guava","mango","cherry","pawpaw"]
listB=["mango","cherry","pawpaw"]

In the lists A and B above, to get the indices of listB elements in listA, I want the indices of "mango","cherry" and "pawpaw" in listA to be [3,4,5] ignoring the first "mango" since it is not part of the consecutive sequence.

Alex
  • 15
  • 6
  • 1
    How big do your lists get, is performance of the code an issue? – Tom S May 03 '21 at 06:01
  • 3
    It is not clear why the linked post does not solve your problem. It is not clear what you tried and did not succeed. – Gulzar May 03 '21 at 06:03
  • 1
    Why does the https://stackoverflow.com/questions/3847386/how-to-test-if-a-list-contains-another-list/3847612#3847612 not answer your question? As far as I see, it's exactly the same question, except that you are working with strings and are asking for explicit indices instead of range. – jurez May 03 '21 at 06:04
  • 1
    Does this answer your question? [How to test if a list contains another list?](https://stackoverflow.com/questions/3847386/how-to-test-if-a-list-contains-another-list) – jurez May 03 '21 at 06:06
  • @Tom S. The maximum length of the main list is 15 – Alex May 03 '21 at 06:12
  • Then I do not see a problem with what everyone else has suggested – Tom S May 03 '21 at 06:13

1 Answers1

1

Keep a sliding window of length equal to the length of listB

for i in range(len(listA)- len(listB) + 1):
    if listA[i:i+len(listB)] == listB:
        print([k for k in range(i,i+len(listB))])
Nk03
  • 14,699
  • 2
  • 8
  • 22