+8

How to find out which string is more matching then others? Python

If I have a list of strings. lst = [] #List of strings string = input() How do I find out which list index matches the best with the string variable? Matches the best. Always returns something. lst = ["solo", "sololearn"] string = "solo" That would return index 0 lst = ["solo", "sololearn"] string = "sololrn" That would return index 1 And if there's only one item it would of course return index 0 since there's no more strings that could match better

7/11/2018 11:27:31 PM

Toni Isotalo

8 Answers

New Answer

+9

The usual way to do this is to calculate the levenshtein distance between your word and all the words in the list. Smallest levenshtein distance means best match. The Levenshtein distance checks how many letters you would need to swap or add or remove to get to the target word. Look it up, it's not too hard to implement!

+7

Schindlabua Wow, thanks. I have learned something. First time I've heard of Levenshtein distance. https://code.sololearn.com/cUCYOx3cC6rd/?ref=app

+4

matches best? or matches a string in your list.

+4

hmmm idk Toni Isotalo

+4

from difflib import SequenceMatcher as SM lst = ["solo", "sololearn"] string = "solo" a = max(lst, key = lambda x:SM(a = string, b = x).ratio()) print(lst.index(a)) lst = ["solo", "sololearn"] string = "sololrn" a = max(lst, key = lambda x:SM(a = string, b = x).ratio()) print(lst.index(a))

+3

take substrings of search string and then match it with list in reverse order (longest substring first).. so it will match the best

+3

Sahil Danayak change your list to list=['apple','banana', 'bandana' ,'mango','manga'] It has difficulty deciding between similar words.

+2

Extremely good question! It should be in a challenge :) my try: https://code.sololearn.com/cngfC80PsFuC/?ref=app