finding all sublists of list2 that are identical to list1
Diez B. Roggisch
nospam-deets at web.de
Mon Feb 9 10:46:13 EST 2004
> - Is there a more efficient way to do it? (Apart from building
> automata.)
Look into the shift-and algorithm for string-search- that should be much
faster. Its not trivial so I can't reproduce it right here out of my head,
but google helps.
> - Is there a more elegant/Pythonic way to write the function?
Yes. You should work much more with slicing, and compare whole sublists like
this:
-------snip-----------
hs = [0, 1, 5, 3, 2, 4, 5, 3, 2]
ne = [5, 3, 2]
def sublist(needle, haystack):
hs_size = len(haystack)
n_size = len(needle)
if n_size > hs_size:
return []
elif hs_size == n_size and needle == haystack:
return []
else:
res = []
offset = 0
sublist = haystack[offset:offset + n_size]
while len(sublist) == n_size:
if sublist == needle:
res.append(offset)
offset += 1
sublist = haystack[offset:offset + n_size]
return res
if __name__ == "__main__":
print sublist(ne, hs)
-------snip-----------
--
Regards,
Diez B. Roggisch
More information about the Python-list
mailing list