Python Program to Extract Mesh Matching Strings


Pattern recognition is an important programming concept. It allows us to retrieve specific data that satisfies a particular condition or match a particular sequence. This principle is helpful in various fields including language and image processing. String matching helps us to extract meaningful information from a large collection of data.

In this article, we will be discussing a similar concept of extracting mesh matching strings from a given list of strings. Mesh matching focuses on the extraction “similar” strings of equal length, let’s discuss this problem in detail.

Understanding the Problem

The main concept is to extract similar strings that follow a specific pattern. The mesh string contains missing characters and those strings that match the hidden pattern of the mesh should be extracted. Let’s understand this with the help of an example −

Input Output Scenarios

Input:
lis1 = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"

The mesh contains the letters “e” and “h” along with some missing characters forming a pattern. We have to extract the strings from the list which matches the mesh pattern.

Output: ['Suresh', 'Ramesh']

Clearly, the words “Suresh” and “Ramesh” matches the mesh pattern. Now that we have understood the problem statement, let’s discuss a few solutions.

Using Iterations along with Zip()

After passing a list of strings and the mesh pattern, we will create an empty list for storing the extracted strings. We will iterate over each string using “for” loop and within the loop we will establish a condition that checks whether or not the length of the current “string” (word) is equal to the length of the mesh.

This allows us to select relevant strings for mesh matching. The condition also checks whether the character in the mesh is an “underscore” or it matches the corresponding character in the word.

Using these two conditions, we will extract the words that match the mesh pattern. If both strings and their characters satisfy the conditions then the zip() function will be used to iterate and pair the characters from “mesh” with the characters of “words”. This function allows a pair-wise comparison between the two patterns.

Example

Following is an example to extract mesh matching strings –

lis1 = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"
lis2 = []
print(f"The original list is: {lis1}")

for words in lis1:
   if (len(words) == len(mesh)) and all((letter1 == "_") or (letter1 == letter2)
      for letter1, letter2 in zip(mesh, words)):
         lis2.append(words)

print(f"The new list is: {lis2}")

Output

The original list is: ['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The new list is: ['Suresh', 'Ramesh']

Using List Comprehension

In this approach, we will use the technique of list comprehension to produce a meticulous and compact code. The multiline concept of iterations can be summarized into a code of few lines.

This compact representation enhances the program’s readability. We will use the same logic of establishing conditions and segregating the values that fits the criteria. Both “for” and “if” loops will be applied within a list. Each character is compared and paired with the help of zip() function.

Example

Following is an example −

lis1 = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"
print(f"The original list is:{lis1}")

lis2 = [words for words in lis1 if (len(words) == len(mesh)) and all((letter1 == "_") or (letter1 == letter2)
           for letter1, letter2 in zip(mesh, words))]

print(f"The new list is: {lis2}")

Output

The original list is:['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The new list is: ['Suresh', 'Ramesh']

Using Filter() + Lambda() Functions

Filter function can be used to filter values based on certain criteria. After providing the mesh pattern and the list of strings, we will create a new list with the help of filter() and lambda functions. The filter function will filter out the non-matching strings and inside it, we will define a lambda function which we will check the length of both the patterns.

The zip() function will be used to compare and pair the characters. The filtering condition will be provided by the lambda function.

Example

Following is an example −

lis1 = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"
print(f"The original list is:{lis1}")

lis2 = list(filter(lambda words: len(words) == len(mesh) and all((letter1 == "_") or (letter1 == letter2)
   for letter1, letter2 in zip(mesh, words)), lis1))
print(f"The new list is: {lis2}")

Output

The original list is:['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The new list is: ['Suresh', 'Ramesh']

Valuable Insights

All the above discussed solutions follow a core principle: In order to compare the strings and the mesh pattern their length should be equal i.e.; both the strings and the mesh should have equal number of characters. Also, the pattern recognition will change if the positions of the underscores change.

Conclusion

During the course of this article, we discussed a few prolific and efficient solutions to extract mesh matching strings. Initially we focused on understanding the concept of mesh matching and later on we covered the solutions. We applied numerous programming concepts including “iterations”, “list comprehension”, “filter()” and “lambda functions” to accomplish our objective.

Updated on: 12-Jul-2023

36 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements