testing if a list contains a sublist

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Tue Aug 16 08:23:44 EDT 2011


Roy Smith <roy at panix.com> writes:

>> what is the best way to check if a given list (lets call it l1) is
>> totally contained in a second list (l2)?

[...]
> import re
>
> def sublist(l1, l2):
>     s1 = ''.join(map(str, l1))
>     s2 = ''.join(map(str, l2))
>     return re.search(s1, s2)

This is complete nonsense (try it on [12] and [1,2]).

The original problem is "string searching" (where strings here are
sequences of numbers instead of characters). See

http://en.wikipedia.org/wiki/String_searching_algorithm

for any algorithm (Rabin-Karp seems appropriate to me).

-- Alain.



More information about the Python-list mailing list