Open In App
Related Articles

Python program to Extract Mesh matching Strings

Improve
Improve
Improve
Like Article
Like
Save Article
Save
Share
Report issue
Report

Given a character mesh, containing missing characters, match the string which matches the mesh.

Example:

Input : test_list = [“geeks”, “best”, “peeks”], mesh = “_ee_s” 
Output : [‘geeks’, ‘peeks’] 
Explanation : Elements according to mesh are geeks and peeks.

Input : test_list = [“geeks”, “best”, “test”], mesh = “_e_t” 
Output : [‘best’, ‘test’] 
Explanation : Elements according to mesh are best and test. 

Method #1: Using loop + all() + len() + zip() 

The combination of the above can be used to solve this problem. In this, we perform the task of matching mesh and each string using zip() and all() is used to check for all strings, len() is used to test for correct string length matching mesh.

Python3

# Python3 code to demonstrate working of
# Extract Mesh matching Strings
# Using len() + loop + zip() + all()
 
# initializing list
test_list = ["geeks", "best", "peeks", "for", "seeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing mesh
mesh = "_ee_s"
 
res = []
for sub in test_list:
     
    # testing for matching mesh, checking each character and length
    if (len(sub) == len(mesh)) and all((ele1 == "_") or (ele1 == ele2)
       for ele1, ele2 in zip(mesh, sub)):
        res.append(sub)
 
# printing result
print("The extracted strings : " + str(res))

                    

Output
The original list : ['geeks', 'best', 'peeks', 'for', 'seeks']
The extracted strings : ['geeks', 'peeks', 'seeks']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method #2: Using list comprehension

This solves the problem in a similar way as the above method, the only difference here is that 1 liner solution is provided. 

Python3

# Python3 code to demonstrate working of
# Extract Mesh matching Strings
# Using list comprehension
 
# initializing list
test_list = ["geeks", "best", "peeks", "for", "seeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing mesh
mesh = "_ee_s"
 
# one liner to solve this problem
res = [sub for sub in test_list if (len(sub) == len(mesh)) and all((ele1 == "_") or (ele1 == ele2)
      for ele1, ele2 in zip(mesh, sub))]
 
# printing result
print("The extracted strings : " + str(res))

                    

Output
The original list : ['geeks', 'best', 'peeks', 'for', 'seeks']
The extracted strings : ['geeks', 'peeks', 'seeks']

Time Complexity: O(n)
Auxiliary Space: O(n)

Method 3: Using the filter() function along with lambda function

  • Start by initializing the list of strings test_list and the mesh string mesh.
  • Create an empty list result to store the strings that match the mesh.
  • Loop through each string s in test_list.
  • Check if the length of s is equal to the length of mesh. If not, move on to the next string.
  • Loop through each character c in s and its corresponding character m in mesh.
  • If m is equal to “_”, move on to the next character. Otherwise, check if c is equal to m. If not, move on to the next string.
  • If the loop completes without finding any mismatches, append the string s to the result list.
  • Once all strings have been checked, return the result list containing the matching strings.

Python3

# Python3 code to demonstrate working of
# Extract Mesh matching Strings
# Using filter() + lambda function
 
# initializing list
test_list = ["geeks", "best", "peeks", "for", "seeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing mesh
mesh = "_ee_s"
 
# filtering the list using filter() and lambda function
res = list(filter(lambda x: len(x) == len(mesh) and all((ele1 == "_") or (ele1 == ele2) for ele1, ele2 in zip(mesh, x)), test_list))
 
# printing result
print("The extracted strings : " + str(res))

                    

Output
The original list : ['geeks', 'best', 'peeks', 'for', 'seeks']
The extracted strings : ['geeks', 'peeks', 'seeks']

Time Complexity: O(n*m), where n is the number of strings in the input list, and m is the length of the mesh.
Auxiliary Space: O(k), where k is the number of strings that match the mesh.

Method #4: Using a for loop and a helper function

Python3

def match_mesh(mesh, string):
    """
    Helper function to check if a given string matches with the mesh.
    """
    if len(mesh) != len(string):
        return False
    for i in range(len(mesh)):
        if mesh[i] != '_' and mesh[i] != string[i]:
            return False
    return True
 
# initializing list
test_list = ["geeks", "best", "peeks", "for", "seeks"]
 
# printing original list
print("The original list : " + str(test_list))
 
# initializing mesh
mesh = "_ee_s"
 
# extracting strings matching the mesh using for loop and helper function
res = []
for string in test_list:
    if match_mesh(mesh, string):
        res.append(string)
 
# printing result
print("The extracted strings : " + str(res))

                    

Output
The original list : ['geeks', 'best', 'peeks', 'for', 'seeks']
The extracted strings : ['geeks', 'peeks', 'seeks']

Time complexity: O(n * m), where n is the number of strings in the list and m is the length of the mesh.
Auxiliary space: O(k), where k is the number of strings that match the mesh.



Last Updated : 01 May, 2023
Like Article
Save Article
Previous
Next
Share your thoughts in the comments
Similar Reads
Complete Tutorials