Skip to content

Latest commit

 

History

History
20 lines (18 loc) · 1.15 KB

fuzzy-string-matching-using-difflib-python.md

File metadata and controls

20 lines (18 loc) · 1.15 KB

Using difflib in Python for fuzzy string matching

Posted on 10 Aug, 2021

>>> from difflib import get_close_matches                                  
>>> fruits = ["apple", "orange", "banana", "peach"]                        
>>> get_close_matches('app', fruits)                                       
['apple']                                                                  
>>> get_close_matches('aple', fruits)                                      
['apple']                                                                  
>>> get_close_matches('ach', fruits)                                       
['peach']                                                                  
>>> get_close_matches('ba', fruits)                                        
[]                                                                         
>>> get_close_matches('ban', fruits)                                       
['banana']  
  • A nice use case for this in CLI when a user enter a wrong sub-command, we can suggest or automatically run the correct command
  • get_close_matches docs