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 processing 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 of "similar" strings of equal length that follow a specific pattern.

Understanding the Problem

The main concept is to extract similar strings that follow a specific pattern. The mesh string contains missing characters (represented by underscores) 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
words = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"
print(f"Input list: {words}")
print(f"Mesh pattern: {mesh}")
Input list: ['Suresh', 'Ramesh', 'Ravi', 'Raghav']
Mesh pattern: ___e_h

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

Clearly, the words "Suresh" and "Ramesh" match the mesh pattern because they have 'e' at position 4 and 'h' at position 6 (both are 6 characters long).

Method 1: Using Iterations with zip()

We will iterate over each string and check if its length matches the mesh length. Then we'll use zip() to pair characters from the mesh with characters from each word for comparison ?

words = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"
result = []

print(f"The original list is: {words}")

for word in words:
    if (len(word) == len(mesh)) and all((mesh_char == "_") or (mesh_char == word_char)
        for mesh_char, word_char in zip(mesh, word)):
        result.append(word)

print(f"The mesh matching strings are: {result}")
The original list is: ['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The mesh matching strings are: ['Suresh', 'Ramesh']

Method 2: Using List Comprehension

This approach uses list comprehension to produce a more compact and readable code. The same logic is applied but in a single line ?

words = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"

print(f"The original list is: {words}")

result = [word for word in words if (len(word) == len(mesh)) and 
          all((mesh_char == "_") or (mesh_char == word_char)
              for mesh_char, word_char in zip(mesh, word))]

print(f"The mesh matching strings are: {result}")
The original list is: ['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The mesh matching strings are: ['Suresh', 'Ramesh']

Method 3: Using filter() with Lambda Functions

The filter() function can be used to filter values based on certain criteria. We'll define a lambda function to check the matching conditions ?

words = ["Suresh", "Ramesh", "Ravi", "Raghav"]
mesh = "___e_h"

print(f"The original list is: {words}")

result = list(filter(lambda word: len(word) == len(mesh) and 
                     all((mesh_char == "_") or (mesh_char == word_char)
                         for mesh_char, word_char in zip(mesh, word)), words))

print(f"The mesh matching strings are: {result}")
The original list is: ['Suresh', 'Ramesh', 'Ravi', 'Raghav']
The mesh matching strings are: ['Suresh', 'Ramesh']

How It Works

The core logic involves two key conditions:

  • Length Check: The string and mesh must have equal length
  • Pattern Matching: Each character in the mesh is either an underscore (wildcard) or must match the corresponding character in the string

The zip() function pairs characters from both strings, and all() ensures every character pair satisfies the matching condition.

Comparison

Method Code Length Readability Best For
For Loop Multiple lines High Learning and debugging
List Comprehension Single line Medium Compact solutions
Filter + Lambda Single line Medium Functional programming

Conclusion

Mesh matching is a powerful pattern recognition technique for extracting strings that follow specific patterns. All three methods achieve the same result use the for loop approach for clarity, list comprehension for conciseness, or filter with lambda for functional programming style.

Updated on: 2026-03-27T07:37:02+05:30

205 Views

Kickstart Your Career

Get certified by completing the course

Get Started
Advertisements