Python program to Extract Mesh matching Strings
Last Updated :
01 May, 2023
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
test_list = [ "geeks" , "best" , "peeks" , "for" , "seeks" ]
print ( "The original list : " + str (test_list))
mesh = "_ee_s"
res = []
for sub in test_list:
if ( len (sub) = = len (mesh)) and all ((ele1 = = "_" ) or (ele1 = = ele2)
for ele1, ele2 in zip (mesh, sub)):
res.append(sub)
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
test_list = [ "geeks" , "best" , "peeks" , "for" , "seeks" ]
print ( "The original list : " + str (test_list))
mesh = "_ee_s"
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))]
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
test_list = [ "geeks" , "best" , "peeks" , "for" , "seeks" ]
print ( "The original list : " + str (test_list))
mesh = "_ee_s"
res = list ( filter ( lambda x: len (x) = = len (mesh) and all ((ele1 = = "_" ) or (ele1 = = ele2) for ele1, ele2 in zip (mesh, x)), test_list))
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):
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
test_list = [ "geeks" , "best" , "peeks" , "for" , "seeks" ]
print ( "The original list : " + str (test_list))
mesh = "_ee_s"
res = []
for string in test_list:
if match_mesh(mesh, string):
res.append(string)
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.
Similar Reads
Python Program to Convert Matrix to String
Program converts a 2D matrix (list of lists) into a single string, where all the matrix elements are arranged in row-major order. The elements are separated by spaces or any other delimiter, making it easy to represent matrix data as a string. Using List ComprehensionList comprehension provides a co
2 min read
Python program to find String in a List
Searching for a string in a list is a common operation in Python. Whether we're dealing with small lists or large datasets, knowing how to efficiently search for strings can save both time and effort. In this article, weâll explore several methods to find a string in a list, starting from the most e
3 min read
Python | Extract odd length words in String
Sometimes, while working with Python, we can have a problem in which we need to extract certain length words from a string. This can be extraction of odd length words from the string. This can have application in many domains including day-day programming. Lets discuss certain ways in which this tas
5 min read
Python - Extract Indices of substring matches
Given a String List, and a substring, extract list of indices of Strings, in which that substring occurs. Input : test_list = ["Gfg is good", "for Geeks", "I love Gfg", "Gfg is useful"], K = "Gfg" Output : [0, 2, 3] Explanation : "Gfg" is present in 0th, 2nd and 3rd element as substring. Input : tes
5 min read
Python Program To Check If A String Is Substring Of Another
Given two strings s1 and s2, find if s1 is a substring of s2. If yes, return the index of the first occurrence, else return -1. Examples :Â Input: s1 = "for", s2 = "geeksforgeeks" Output: 5 Explanation: String "for" is present as a substring of s2. Input: s1 = "practice", s2 = "geeksforgeeks" Output
3 min read
Python - Extract indices of Present, Non Index matching Strings
Given two strings, extract indices of all characters from string 1 which are present in the other string, but not in the same index. Input : test_str1 = 'pplg', test_str2 = 'pineapple' Output : [0, 1, 2] Explanation : ppl is found in 2nd string, also not on same index as 1st. Input : test_str1 = 'pi
6 min read
Python - Extracting Key from Value Substring
Sometimes, while working with Python dictionaries, we can have a problem in which we need to find the key from given value, querying substring from key's value. This kind of problem is common and have application in many domains including web development. Lets discuss certain ways in which this task
5 min read
Python - Extract K length substrings
The task is to extract all possible substrings of a specific length, k. This problem involves identifying and retrieving those substrings in an efficient way. Let's explore various methods to extract substrings of length k from a given string in Python Using List Comprehension List comprehension is
3 min read
Python Program To Get Minimum Elements For String Construction
Given a String, the task is to write a Python program to count the minimum elements required to form a string from list elements. Input : test_list = ["geek", "ring", "sfor", "ok", "woke"], tar_str = "working" Output : 2 Explanation : working can be formed using woke and ring. Input : test_list = ["
3 min read
Python | Extract words from given string
In Python, we sometimes come through situations where we require to get all the words present in the string, this can be a tedious task done using the native method. Hence having shorthand to perform this task is always useful. Additionally, this article also includes the cases in which punctuation
5 min read