There's nothing wrong with writing your own code to find the longest common substring, but are you aware that python has a module in the standard library that already does this? In the difflib module, the SequenceMatcher class can compare two sequences and extract the longest common sequence of elements from it, like this:
Code: import difflib a = [1, 2, 3, 7] b = [2, 3, 7] seq_matcher = difflib.SequenceMatcher(None, a, b) print seq_matcher.find_longest_match(0, len(a), 0, len(b)) Outputs: Match(a=1, b=0, size=3) See http://docs.python.org/library/difflib.html#sequencematcher-objects for lots of details. The SequenceMatcher class can do a lot more than finding the common substrings, as you might expect. The Module of the Week article for difflib may also be of interest: http://www.doughellmann.com/PyMOTW/difflib/index.html -- Jerry
_______________________________________________ Tutor maillist - Tutor@python.org To unsubscribe or change subscription options: http://mail.python.org/mailman/listinfo/tutor