title: Retrieve the longest matching value from a list by matching with another list [Python 2.7]
body: <p>There are two lists to be matched, <code>li_a</code> is the given list consists of sequence of characters of a sentence, whereas <code>li_b</code> is the collection of words.</p>
  
  <pre><code>li_a = ['T','h','e','s','e','45','a','r','e','c','a','r','s']
  
  
  li_b = ['T','Th','The','Thes','These','a','ar','are','c','ca','car','cars']
  </code></pre>
  
  <p>The process is to match items of <code>li_a</code> iteratively, with <code>li_b</code> items. If the first character of <code>li_a</code> is similar to <code>li_b</code> items, the first character of <code>li_a</code> joins with next character, and redo the process until it reaches to its longest match. Then, the longest term should be split, and the process will continue till the end. As the unknown characters and words of <code>li_a</code> which do not appear in <code>li_b</code> will be appended as they were before.</p>
  
  <p>The final work should be like this:</p>
  
  <pre><code>new_li = ['These','45','are','cars']
  </code></pre>
  
  <p>The attempt getting so far, but this works for <strong>two strings</strong> not for <strong>Lists</strong>, and it doesn't retrieve unidentified words.</p>
  
  <pre><code>def longest_matched_substring(s1, s2):
      m = [[0] * (1 + len(s2)) for i in xrange(1 + len(s1))]
      longest, x_longest = 0, 0
      for x in xrange(1, 1 + len(s1)):
         for y in xrange(1, 1 + len(s2)):
             if s1[x - 1] == s2[y - 1]:
                 m[x][y] = m[x - 1][y - 1] + 1
                 if m[x][y] &gt; longest:
                     longest = m[x][y]
                     x_longest = x
             else:
                 m[x][y] = 0
  
      return s1[x_longest - longest: x_longest]
  </code></pre>