Home  >  Article  >  Backend Development  >  Python program to extract grid matching strings

Python program to extract grid matching strings

WBOY
WBOYforward
2023-09-09 22:21:09789browse

Python program to extract grid matching strings

Pattern recognition is an important programming concept. It allows us to retrieve specific data that meets specific conditions or matches a specific sequence. This principle is helpful in various fields such as language and image processing. String matching helps us extract meaningful information from large amounts of data.

In this article, we will discuss the similar concept of extracting grid matching strings from a given list of strings. The focus of grid matching is to extract equal-length "similar" strings. Let's discuss this issue in detail.

Understanding Questions

The main concept is to extract similar strings that follow a specific pattern. The grid string contains missing characters, and those strings that match the grid's hidden pattern should be extracted. Let us understand this through an example -

Input and output scenarios

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

grid contains the letters "e" and "h" as well as some missing characters that form a pattern. We have to extract the string matching the grid pattern from the list.

Output: ['Suresh', 'Ramesh']

Apparently, the words "Suresh" and "Ramesh" match the grid pattern. Now that we understand the problem statement, let's discuss some solutions.

Using iteration with Zip()

After passing the list of strings and the grid pattern, we will create an empty list to store the extracted strings. We will use a "for" loop to iterate over each string and create a condition within the loop to check if the length of the current "String" (word) is equal to the length of the string. net.

This allows us to select relevant strings for grid matching. The condition also checks whether the characters in the grid are "underscore" or match the corresponding characters in the word.

Using these two conditions, we will extract words that match the grid pattern. If both the string and its characters meet the conditions, the zip() function will be used to iterate and pair the characters in "mesh" with the characters in "" ". This function allows pairwise comparison of two patterns.

Example

The following is an example of extracting a grid match string –

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']

Use list comprehension

In this approach, we will use list comprehension techniques to generate detailed and compact code. The multi-line concept of iteration can be summarized into a few lines of code.

This compact representation enhances program readability. We will use the same logic to establish conditions and separate values ​​that meet the criteria. Both "for" and "if" loops will be applied to lists. Each character is compared and matched with the help of zip() function.

Example

The 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']

Use Filter() Lambda() function

The filter function can be used to filter values ​​based on specific criteria. After providing the grid pattern and list of strings, we will create a new list with the help of filter() and lambda functions. The filter function will filter out unmatched strings and define a lambda function within it that checks the length of both patterns.

zip() The function will be used to compare and match characters. The filter criteria will be provided by the lambda function.

Example

The 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 one core principle: in order to compare string and grid patterns, their lengths should be equal, i.e.; string and grid should have the same number of characters. Additionally, if the position of the underline changes, the pattern recognition changes as well.

in conclusion

In this article, we discussed some rich and efficient solutions to extract grid matching strings. Initially we focused on understanding the concept of grid matching and later we introduced solutions. We applied many programming concepts, including "Iteration", "List comprehension", "filter()" and "lambda function ” b>” to achieve our goals.

The above is the detailed content of Python program to extract grid matching strings. For more information, please follow other related articles on the PHP Chinese website!

Statement:
This article is reproduced at:tutorialspoint.com. If there is any infringement, please contact admin@php.cn delete