testing if a list contains a sublist
Laszlo Nagy
gandalf at shopzeus.com
Tue Aug 16 10:00:15 CEST 2011
> Error free? Consider this stated requirement:
>> l1 = [1,2,2,], l2 = [1,2,3,4,5] -> l1 is not contained in l2
If you look it the strict way, "containment" relation for lists is meant
this way:
l1 = []
l2 = [1,l1,2] # l2 CONTAINS l1
But you are right, I was wrong. So let's clarify what the OP wants!
For example:
l1 = [1,2,2,], l2 = [2,1,2,3,4,5]
What is the relation between these two lists? Does l2 contain l1 or not?
In other words, is this "containment" relation interpreted on multisets
not considering the order of the items?
>
> It also completely ignores list order, which would make [9,8,7] a
> sublist of [5,6,7,8,9].
Exactly. However, from the original post of Johannes it was not clear if
the order of the elements counts or not.
If It this is interpreted as a multiset relation, it would be easier to
use collections.Counter. If the order of elements is important then he
can start with a Boyer-Moore algorithm.
Best,
Laszlo
More information about the Python-list
mailing list