- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Find all close matches of input string from a list in Python
Suppose we are given a word and we want to find its closest matches. Not an exact match but other words which have some close resemblance in pattern with the given word. For this we use a module called difflib and use its method named get_close_matches.
get_close_matches
This method is part of the module difflib and gives us the match with possible patterns which we specify. Below is the syntax.
difflib.get_close_matches(word, possibilities, n, cutoff) word: It is the word to which we need to find the match. Possibilities: This is the patterns which will be compared for matching. n: Maximum number of close matches to return. Should be greater than 0. Cutoff: The possibilities that do not score this float value between 0 and 1 are ignored.
Running the above code gives us the following result −
Example
In the below example we take a word and also a list of possibilities or patterns that need to be compared. Then we apply the method to get the result needed.
from difflib import get_close_matches word = 'banana' patterns = ['ana', 'nana', 'ban', 'ran','tan'] print('matched words:',get_close_matches(word, patterns))
Output
Running the above code gives us the following result −
matched words: ['nana', 'ban', 'ana']
- Related Articles
- Python program to find all close matches of input string from a list
- Java program to find all close matches of input string from a list
- Python Check if suffix matches with any string in given list?
- How can I find all matches to a regular expression in Python?
- Removing all collections whose name matches a string in MongoDB
- Find All Duplicate Characters from a String using Python
- Python Get a list as input from user
- Getting the list of all the matches Java regular expressions
- Program to find list of all possible combinations of letters of a given string s in Python
- Return real parts if input is complex with all imaginary parts close to zero in Python
- How to close a Python figure by keyboard input using Matplotlib?
- Create a tuple from string and list in Python
- Remove all duplicates from a given string in Python
- Insert the string at the beginning of all items in a list in Python
- Python program to find the String in a List

Advertisements