简体   繁体   中英

Find word with most matching letters

I have 2 lists:

l1 = ['UK', 'GER', 'POL']

and

l2 = ['Germany', 'Poland', 'United Kingdom']

The lists are referring to countries but don't match in order.

Is there a way to match the values in l1 to l2 based on the number of letters in l2 ?

So the output is:

dic = {'UK': 'United Kingdom',
       'GER': 'Germany',
       'POL': 'Poland'}

Something like this may work for you:

#!/usr/bin/env python3

l1 = ['UK', 'GER', 'POL']
l2 = ['Germany', 'Poland', 'United Kingdom', 'Ukraine']

d = {}
for short in l1:
    lower = short.lower()

    # Match prefix or initials.
    matches = [x for x in l2 if
               x.lower().startswith(lower) or
               ''.join(w[0] for w in x.split()).lower() == lower]

    if len(matches) == 0:
        print('no match', short)
    elif len(matches) > 1:
        print('ambiguous', short, matches)
    else:
        d[short] = matches[0]

print(d)
$ ./test.py 
ambiguous UK ['United Kingdom', 'Ukraine']
{'GER': 'Germany', 'POL': 'Poland'}

I added "Ukraine" to test handling ambiguous matches.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM