Compare List Similarity Python
-
20-03-2021 - |
Question
S = ['hom']
L = ['home','honda','Hammer','Elephant']
I want to get output to show similarity
hom = home
and
print home
How I can do this? I want to use approximate matching to change "hom" to "home".
Solution
For something built-in, you have difflib.
It can (as said in the docs): Return a list of the best “good enough” matches. word is a sequence for which close matches are desired (typically a string), and possibilities is a list of sequences against which to match word (typically a list of strings).
The example in the docs is:
>>> from difflib import get_close_matches
>>> get_close_matches('appel', ['ape', 'apple', 'peach', 'puppy'])
['apple', 'ape']
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow