Reputation: 145
I have a list of strings:
list = ["END_BOOTS", "END_CHEST", "MIDAS_SWORD", "SLIME_HAT", "WOOD", "ENDER_PEARL"]
and a keyword:
keyword = "ENDER_BOOTS"
I need to scan the list to search for a value that is the closest to the keyword. In this case the closest value would be END_BOOTS. In case when such value can not be found, the code should return false.
I've tried turning each individual value into a list of characters, sorting it and doing the same to the keyword. After I would compare them and check which list of characters has more common letters with the keyword. However that doesn't seem to work very well and it is not very efficient.
What would be a good solution to this problem in python 3?
Upvotes: 3
Views: 216
Reputation: 6802
I'd suggest calculating the edit distance between your search keyword and every item in the list, then taking the minimum as your match.
import editdistance
item_list = ["END_BOOTS", "END_CHEST", "MIDAS_SWORD", "SLIME_HAT", "WOOD", "ENDER_PEARL"]
keyword = "SLIMER_HUT"
distances = [editdistance.eval(keyword, item) for item in item_list]
match_index = distances.index(min(distances))
print(distances)
# [10, 9, 10, 2, 10, 9]
print(item_list[match_index])
# SLIME_HAT
Upvotes: 1
Reputation: 337
import difflib
a = 'ENDER_BOOTS'
b = 'END_BOOTS'
seq = difflib.SequenceMatcher(None,a,b)
d = seq.ratio()*100
print(d)
Loop over the list and return the word that gets the highest score
Upvotes: 4