Python Program to Extract Strings with at least given number of characters from other list
Last Updated :
16 May, 2023
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.
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))
OutputThe 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))
OutputThe 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))
OutputThe 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))
OutputThe 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
- Import the itertools module.
- Initialize a list called "test_list" with some strings.
- Print the original list using the "print()" function.
- Initialize another list called "char_list" with some characters.
- Initialize a variable "K" with a value of 2.
- 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".
- Print the filtered strings using the "print()" function.
Python3
# 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))
OutputThe 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 the re module to work with regular expressions.
- Initialize an empty list res to store the filtered strings.
- Compile a regular expression pattern using the re.compile() method that matches any combination of characters from char_list of length K.
- Loop through each string s in test_list.
- Use the re.findall() method to find all the matches of the pattern in s. If the length of the resulting list is greater than zero, it means that at least one combination of characters was found in s.
- Append s to res if at least one combination of characters was found in s.
- Return res.
Python3
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))
OutputFiltered 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.
Similar Reads
Python Tutorial | Learn Python Programming Language
Python Tutorial â Python is one of the most popular programming languages. Itâs simple to use, packed with features and supported by a wide range of libraries and frameworks. Its clean syntax makes it beginner-friendly.Python is:A high-level language, used in web development, data science, automatio
10 min read
Python Interview Questions and Answers
Python is the most used language in top companies such as Intel, IBM, NASA, Pixar, Netflix, Facebook, JP Morgan Chase, Spotify and many more because of its simplicity and powerful libraries. To crack their Online Assessment and Interview Rounds as a Python developer, we need to master important Pyth
15+ min read
Python OOPs Concepts
Object Oriented Programming is a fundamental concept in Python, empowering developers to build modular, maintainable, and scalable applications. By understanding the core OOP principles (classes, objects, inheritance, encapsulation, polymorphism, and abstraction), programmers can leverage the full p
11 min read
Python Projects - Beginner to Advanced
Python is one of the most popular programming languages due to its simplicity, versatility, and supportive community. Whether youâre a beginner eager to learn the basics or an experienced programmer looking to challenge your skills, there are countless Python projects to help you grow.Hereâs a list
10 min read
Python Exercise with Practice Questions and Solutions
Python Exercise for Beginner: Practice makes perfect in everything, and this is especially true when learning Python. If you're a beginner, regularly practicing Python exercises will build your confidence and sharpen your skills. To help you improve, try these Python exercises with solutions to test
9 min read
Python Programs
Practice with Python program examples is always a good choice to scale up your logical understanding and programming skills and this article will provide you with the best sets of Python code examples.The below Python section contains a wide collection of Python programming examples. These Python co
11 min read
Enumerate() in Python
enumerate() function adds a counter to each item in a list or other iterable. It turns the iterable into something we can loop through, where each item comes with its number (starting from 0 by default). We can also turn it into a list of (number, item) pairs using list().Let's look at a simple exam
3 min read
Python Data Types
Python Data types are the classification or categorization of data items. It represents the kind of value that tells what operations can be performed on a particular data. Since everything is an object in Python programming, Python data types are classes and variables are instances (objects) of thes
9 min read
Python Introduction
Python was created by Guido van Rossum in 1991 and further developed by the Python Software Foundation. It was designed with focus on code readability and its syntax allows us to express concepts in fewer lines of code.Key Features of PythonPythonâs simple and readable syntax makes it beginner-frien
3 min read
Input and Output in Python
Understanding input and output operations is fundamental to Python programming. With the print() function, we can display output in various formats, while the input() function enables interaction with users by gathering input during program execution. Taking input in PythonPython input() function is
8 min read