Python Program to print strings based on the list of prefix
Last Updated :
22 Apr, 2023
Given a Strings List, the task here is to write a python program that can extract all the strings whose prefixes match any one of the custom prefixes given in another list.
Input : test_list = [“geeks”, “peeks”, “meeks”, “leeks”, “mean”], pref_list = [“ge”, “ne”, “me”, “re”]
Output : [‘geeks’, ‘meeks’, ‘mean’]
Explanation : geeks, meeks and mean have prefix, ge, me and me respectively, present in prefix list.
Input : test_list = [“geeks”, “peeks”, “meeks”, “leeks”, “mean”], pref_list = [“ge”, “le”, “me”, “re”]
Output : [‘geeks’, ‘meeks’, ‘mean’, leeks]
Explanation : geeks, meeks, leeks and mean have prefix, ge, me, me and le respectively, present in prefix list.
Method 1: Using list comprehension, any() and startswith()
In this, we check all elements to match using any() and startswith() extracts all the prefixes. List comprehension is used to iterate through all the strings in the list.
Python3
test_list = [ "geeks" , "peeks" , "meeks" , "leeks" , "mean" ]
print ( "The original list is : " + str (test_list))
pref_list = [ "ge" , "ne" , "me" , "re" ]
res = [ele for ele in test_list if any (ele.startswith(el) for el in pref_list)]
print ( "The extracted prefix strings list : " + str (res))
|
Output
The original list is : ['geeks', 'peeks', 'meeks', 'leeks', 'mean']
The extracted prefix strings list : ['geeks', 'meeks', 'mean']
Time Complexity: O(n * m), where n is the number of elements in the test_list and m is the number of elements in the pref_list.
Auxiliary Space: O(n), where n is the number of elements in the result list ‘res’.
Method 2 : Using filter(), lambda, any() and startswith()
This is similar to the above method, the difference being that filtering is performed using filter() and lambda.
Python3
test_list = [ "geeks" , "peeks" , "meeks" , "leeks" , "mean" ]
print ( "The original list is : " + str (test_list))
pref_list = [ "ge" , "ne" , "me" , "re" ]
res = list ( filter ( lambda ele: any (ele.startswith(el)
for el in pref_list), test_list))
print ( "The extracted prefix strings list : " + str (res))
|
Output
The original list is : ['geeks', 'peeks', 'meeks', 'leeks', 'mean']
The extracted prefix strings list : ['geeks', 'meeks', 'mean']
Time Complexity : O(n2)
Auxiliary Space : O(n)
Method 3 : Using find() method
Python3
test_list = [ "geeks" , "peeks" , "meeks" , "leeks" , "mean" ]
print ( "The original list is : " + str (test_list))
pref_list = [ "ge" , "ne" , "me" , "re" ]
res = []
for i in test_list:
for j in pref_list:
if (i.find(j) = = 0 ):
res.append(i)
print ( "The extracted prefix strings list : " + str (res))
|
Output
The original list is : ['geeks', 'peeks', 'meeks', 'leeks', 'mean']
The extracted prefix strings list : ['geeks', 'meeks', 'mean']
Time complexity: O(n^2), where n is the length of the test_list.
Auxiliary space: O(m), where m is the length of the res list (the space required to store the extracted prefix strings).
Method 4 : Using for loop, len() method and slicing
Python3
test_list = [ "geeks" , "peeks" , "meeks" , "leeks" , "mean" ]
print ( "The original list is : " + str (test_list))
pref_list = [ "ge" , "ne" , "me" , "l" ]
res = []
for i in test_list:
for j in pref_list:
if (i[: len (j)] = = j):
res.append(i)
print ( "The extracted prefix strings list : " + str (res))
|
Output
The original list is : ['geeks', 'peeks', 'meeks', 'leeks', 'mean']
The extracted prefix strings list : ['geeks', 'meeks', 'leeks', 'mean']
Time complexity: O(n*m), where n is the length of test_list and m is the length of pref_list.
Auxiliary space: O(k), where k is the length of res.
Method 7: Using regex
Step-by-step approach:
Import the re module to use regular expressions.
Define a regular expression pattern that matches any of the prefixes in pref_list, using the | (or) operator to separate them.
Use the re.findall() function to find all matches of the pattern in each string in test_list.
Use the any() function to check if there is at least one match for each string in test_list.
If a string has at least one match, add it to the result list, res.
Print the res list.
Python3
import re
test_list = [ "geeks" , "peeks" , "meeks" , "leeks" , "mean" ]
print ( "The original list is : " + str (test_list))
pref_list = [ "ge" , "ne" , "me" , "l" ]
pattern = '|' .join(pref_list)
res = []
for string in test_list:
matches = re.findall(pattern, string)
if any (matches):
res.append(string)
print ( "The extracted prefix strings list : " + str (res))
|
Output
The original list is : ['geeks', 'peeks', 'meeks', 'leeks', 'mean']
The extracted prefix strings list : ['geeks', 'meeks', 'leeks', 'mean']
Time complexity: O(n*m), where n is the length of test_list and m is the length of pref_list.
Auxiliary space: O(n), where n is the length of test_list. This is because we store the result in a list that may contain up to n items.
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...