I have two lists, A
& B
, and I would like to test whether A
is contained in B
. By "contained" I mean that the elements of A
appear in the exact same order within B
with no other elements between them. What I'm looking for is very similar to the behavior of A in B
if they were strings.
Some elements of A
will be repeated. We can assume A
will be shorter than B
.
There are many answers to similar questions on SO, but most answer a different question:
- Is
A
an element ofB
? (Not my question:B
is a flat list, not a list of lists.) - Are all the elements of
A
contained inB
? (Not my question: I'm concerned about order as well.) - Is
A
a sublist ofB
? (Not my question: I don't want to know whether the elements ofA
appear in the same order inB
, I want to know if they appear exactly as they are somewhere inB
.)
If the operation were implemented as the keyword containedin
, it would behave like this.
>>> [2, 3, 4] containedin [1, 2, 3, 4, 5]
True
>>> [2, 3, 4] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 3, 4] containedin [5, 4, 3, 2, 1]
False
>>> [2, 2, 2] containedin [1, 2, 3, 4, 5]
False
>>> [2, 2, 2] containedin [1, 1, 2, 2, 3, 3, 4, 4, 5, 5]
False
>>> [2, 2, 2] containedin [1, 1, 1, 2, 2, 2, 3, 3, 3]
True
Is there a concise way to perform this operation in Python? Am I missing some important terminology that would have led me to the answer more quickly?