Open In App

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.






# 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))

Output
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:




# 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))

Output
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.




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))

Output
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.




# 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))

Output
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.

Method 5: use the itertools.product() function

  1. Import the itertools module.
  2. Initialize a list called “test_list” with some strings.
  3. Print the original list using the “print()” function.
  4. Initialize another list called “char_list” with some characters.
  5. Initialize a variable “K” with a value of 2.
  6. Use a list comprehension to filter out the strings from “test_list” that contain any combination of characters in “char_list” of length “K” or greater. This is done using the “itertools.product()” function to generate all possible combinations of length “K” from “char_list” and the “str.find()” method to check if any of these combinations are in the current string being processed in the list comprehension. The filtered strings are stored in a list called “res”.
  7. Print the filtered strings using the “print()” function. 




# import itertools module
import itertools
 
# 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 itertools.product() function and str.find() method to filter strings
res = [ele for ele in test_list if any(ele.find(
    ''.join(combo)) != -1 for combo in itertools.product(char_list, repeat=K))]
 
# printing result
print("Filtered Strings : " + str(res))

Output
The original list is : ['Geeksforgeeks', 'is', 'best', 'for', 'geeks']
Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']

Time complexity: O(N*M^K), where N is the length of the test_list, M is the length of the char_list, and K is the length of the combination. 
Auxiliary space: O(N).

Method 6: Using regular expressions




import itertools
import re
 
test_list = ["Geeksforgeeks", "is", "best", "for", "geeks"]
char_list = ['e', 't', 's', 'm', 'n']
K = 2
 
res = []
pattern = re.compile('|'.join(''.join(c) for c in itertools.product(char_list, repeat=K)))
 
for s in test_list:
    if len(pattern.findall(s)) > 0:
        res.append(s)
 
print("Filtered Strings : " + str(res))

Output
Filtered Strings : ['Geeksforgeeks', 'best', 'geeks']

Time complexity: O(nm), where n is the length of test_list and m is the maximum length of a string in test_list. 
Auxiliary space: O(n), since we’re storing the filtered strings in res.


Article Tags :