Python – Remove strings with any non-required character
Given a Strings list, remove string, if it contains any unwanted character.
Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], chr_list = [‘f’, ‘m’, ‘n’, ‘i’]
Output : [‘best’, ‘geeks’]
Explanation : Given Strings don’t contain f, m, n or i.Input : test_list = [“gfg”, “is”, “best”, “for”, “geeks”], chr_list = [‘f’, ‘m’, ‘n’]
Output : [‘best’, ‘geeks’, ‘is’]
Explanation : Given Strings don’t contain f, m or n.
Method #1 : Using list comprehension + any()
In this, we test all strings and use any() to iterate all the non-required characters list and check its presence in string, if found, that list is not included in result list.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using list comprehension + any() # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # removing if contains even 1 character res = [sub for sub in test_list if not any (ele in sub for ele in chr_list)] # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['best', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + any()
In this, we perform task of filtering using filter() and lambda function. Rest any() is used to check for any character present in strings for its removal.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using filter() + lambda + any() # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # filter and lambda used to do this task res = list ( filter ( lambda sub: not any ( ele in sub for ele in chr_list), test_list)) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['best', 'geeks']
Time Complexity: O(n) where n is the number of elements in the list “test_list”. filter() and lambda function performs n number of operations.
Auxiliary Space: O(1), no extra space is required
Method #3 : Using replace() method
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings x = [] for i in test_list: for j in chr_list: i = i.replace(j,"") x.append(i) res = [] for i in range ( 0 , len (test_list)): if (test_list[i] = = x[i]): res.append(test_list[i]) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['best', 'geeks']
Method#4: Using Recursive method.
Algorithm:
- Define a function remove_strings that takes a list of strings lst and a list of non-required characters chr_list as input.
- Check if the input list lst is empty, if yes then return an empty list.
- Otherwise, get the first string in the input list lst.
- Recursively call the remove_strings function on the rest of the input list lst.
- Check if the first string first contains any non-required character from the chr_list.
- If yes, exclude the string first from the result and return the remaining modified list rest.
- If no, include the string first in the result and return the modified list rest with first added to the front.
Python3
# Python3 code to demonstrate working of # Remove strings with any non-required character # Using recursive method def remove_strings(lst, chr_list): if not lst: # base case for empty list return [] else : # get the first string in the list first = lst[ 0 ] # recursively remove strings from the rest of the list rest = remove_strings(lst[ 1 :], chr_list) # check if the first string contains any non-required character if any (ele in first for ele in chr_list): # if yes, don't include it in the result return rest else : # otherwise, include it in the result return [first] + rest # initializing list test_list = [ "gfg" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # non-required char list chr_list = [ 'f' , 'm' , 'n' , 'i' ] # checking for all strings # removing if contains even 1 character res = remove_strings(test_list,chr_list) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['gfg', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['best', 'geeks']
The time complexity of this algorithm is O(nmk), where n is the length of the input list lst, m is the average length of the strings in lst, and k is the length of the chr_list. This is because we are looping through the input list lst once, and for each string in lst, we are checking for the presence of any non-required character from chr_list, which takes O(mk) time.
The auxiliary space of this algorithm is also O(nm), because we are creating a new list of modified strings of the same length as the input list. However, this algorithm has a recursive structure, so it may use additional space on the call stack.
Please Login to comment...