Python Program to Extract Strings with at least given number of characters from other list
Given a list containing only string elements, the task is to write a Python program to extract all the strings which have characters from another list given a number of times.
Examples:
Input : test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"], char_list = ['e', 't', 's', 'm', 'n'], K = 2 Output : ['Geeksforgeeks', 'best', 'geeks'] Explanation : is has 1, for has 0 characters from list, hence omitted.
Input : test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"], char_list = ['t', 's', 'm', 'n'], K = 2 Output : ['Geeksforgeeks', 'best'] Explanation : Geeksforgeeks has 2 s, and best has 2 elements from character list.
Method 1: Using list comprehension and sum()
In this, we perform an iteration of characters using list comprehension and sum() is used to check for matching characters sum, which, if found greater than K, is added to result list.
Program:
Python3
# initializing list test_list = [ "Geeksforgeeks" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing characters list char_list = [ 'e' , 't' , 's' , 'm' , 'n' ] # initializing K K = 2 # sum() computes matching elements frequency res = [ele for ele in test_list if sum (ch in char_list for ch in ele) > = K] # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['Geeksforgeeks', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']
Time Complexity: O(n2)
Auxiliary Space: O(n)
Method 2 : Using filter(), lambda and sum()
In this, the task of filtering is performed using filter(), rest all the functionalities is performed using similar constructs as above method.
Program:
Python3
# initializing list test_list = [ "Geeksforgeeks" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing characters list char_list = [ 'e' , 't' , 's' , 'm' , 'n' ] # initializing K K = 2 # sum() computes matching elements frequency # filter() used for task of filtering res = list ( filter ( lambda ele: sum (ch in char_list for ch in ele) > = K, test_list)) # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['Geeksforgeeks', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']
Time Complexity: O(n2) (loop+filter)
Auxiliary Space: O(n)
Approach 3: Using Counter and List Comprehension
This method uses Counter from collections module to compute the frequency of characters in the string and then using list comprehension to extract strings having frequency greater than or equal to K.
Python3
from collections import Counter # initializing list test_list = [ "Geeksforgeeks" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing characters list char_list = [ 'e' , 't' , 's' , 'm' , 'n' ] # initializing K K = 2 # using Counter to compute frequency of characters # and then filtering using list comprehension res = [ele for ele in test_list if sum (Counter(ele)[ch] for ch in char_list) > = K] # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['Geeksforgeeks', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']
Time Complexity: O(n * m) (m is the length of the longest string and n is the number of strings in the list)
Auxiliary Space: O(n)
Method 4: Using a for loop and a set to keep track of the characters in the character list.
This approach uses a for loop to iterate over the strings in the test_list, and another for loop to iterate over the characters in the char_list. It keeps track of the count of each character in the current string using the count() method, and adds it to a running total. Once the count of characters in the current string is greater than or equal to K, the string is added to the result list using the append() method, and the inner for loop is terminated using the break keyword.
Python3
# initializing list test_list = [ "Geeksforgeeks" , "is" , "best" , "for" , "geeks" ] # printing original list print ( "The original list is : " + str (test_list)) # initializing characters list char_list = [ 'e' , 't' , 's' , 'm' , 'n' ] # initializing K K = 2 # using a for loop and a set to filter strings res = [] for ele in test_list: count = 0 for ch in char_list: count + = ele.count(ch) if count > = K: res.append(ele) break # printing result print ( "Filtered Strings : " + str (res)) |
The original list is : ['Geeksforgeeks', 'is', 'best', 'for', 'geeks'] Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']
Time complexity: O(n*m), where n is the number of strings in the test_list and m is the maximum length of a string in the list.
Auxiliary space: O(1), as we only use a constant amount of additional memory to keep track of the count of characters.
Please Login to comment...