How to find the most similar word in a list in python

前端 未结 1 1833
日久生厌
日久生厌 2020-12-06 01:19

I have a list of words

list = [\'car\', \'animal\', \'house\', \'animation\']

and I want to compare every list item with a string str

相关标签:
1条回答
  • 2020-12-06 01:54

    Use difflib:

    difflib.get_close_matches(word, ['car', 'animal', 'house', 'animation'])
    

    As you can see from perusing the source, the "close" matches are sorted from best to worst.

    >>> import difflib
    >>> difflib.get_close_matches('anlmal', ['car', 'animal', 'house', 'animation'])
    ['animal']
    
    0 讨论(0)
提交回复
热议问题