Given two python lists of same length. How to return the best matches of similar values?
Question: Given are two python lists with strings in them (names of persons):

    
    
    list_1 = ['J. Payne', 'George Bush', 'Billy Idol', 'M Stuart', 'Luc van den Bergen']
    list_2 = ['John Payne', 'George W. Bush', 'Billy Idol', 'M. Stuart', 'Luc Bergen']
    

I want a mapping of the names, that are most similar.

    
    
    'J. Payne'           -> 'John Payne'
    'George Bush'        -> 'George W. Bush'
    'Billy Idol'         -> 'Billy Idol'
    'M Stuart'           -> 'M. Stuart'
    'Luc van den Bergen' -> 'Luc Bergen'
    

Is there a neat way to do this in python? The lists contain in average 5 or 6
Names. Sometimes more, but this is seldom. Sometimes it is just one name in
every list, which could be spelled slightly different.


Answer: Using the function defined here:
<http://hetland.org/coding/python/levenshtein.py>

    
    
    >>> for i in list_1:
    ...     print i, '==>', min(list_2, key=lambda j:levenshtein(i,j))
    ... 
    
    
    
    J. Payne ==> John Payne
    George Bush ==> George W. Bush
    Billy Idol ==> Billy Idol
    M Stuart ==> M. Stuart
    Luc van den Bergen ==> Luc Bergen
    

You could use functools.partial instead of the lambda

    
    
    >>> from functools import partial
    >>> for i in list_1:
    ...     print i, '==>', min(list_2, key=partial(levenshtein,i))
    ...
    
    
    
    J. Payne ==> John Payne
    George Bush ==> George W. Bush
    Billy Idol ==> Billy Idol
    M Stuart ==> M. Stuart
    Luc van den Bergen ==> Luc Bergen
    

