Python – Filter above Threshold size Strings
Sometimes, while working with huge amounts of data, we can have a problem in which we need to extract just specific-sized strings above a minimum threshold. This kind of problem can occur during validation cases across many domains. Let’s discuss certain ways to handle this in Python strings list.
Method #1: Using list comprehension + len() The combination of above functionalities can be used to perform this task. In this, we iterate for all the strings and return only above threshold strings checked using len().
Python3
# Python3 code to demonstrate working of # Filter above Threshold size Strings # using list comprehension + len() # initialize list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ] # printing original list print ( "The original list : " + str (test_list)) # initialize Threshold thres = 4 # Filter above Threshold size Strings # using list comprehension + len() res = [ele for ele in test_list if len (ele) > = thres] # printing result print ( "The above Threshold size strings are : " + str (res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The above Threshold size strings are : ['best', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2: Using filter() + lambda The combination of the above functionalities can be used to perform this task. In this, we extract the elements using filter() and logic is compiled in a lambda function.
Python3
# Python3 code to demonstrate working of # Filter above Threshold size Strings # using filter() + lambda # initialize list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ] # printing original list print ( "The original list : " + str (test_list)) # initialize Threshold thres = 4 # Filter above Threshold size Strings # using filter() + lambda res = list ( filter ( lambda ele: len (ele) > = thres, test_list)) # printing result print ( "The above Threshold size strings are : " + str (res)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The above Threshold size strings are : ['best', 'geeks']
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #3: Using copy() + remove() + join() methods
Python3
# Python3 code to demonstrate working of # Filter above Threshold size Strings # initialize list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ] # printing original list print ( "The original list : " + str (test_list)) # initialize Threshold thres = 4 for i in test_list.copy(): if len (i) < thres: test_list.remove(i) # printing result print ( "The above Threshold size strings are : " + ' ' .join(test_list)) |
The original list : ['gfg', 'is', 'best', 'for', 'geeks'] The above Threshold size strings are : best geeks
Time Complexity: O(n) where n is the number of elements in the list “test_list”.copy() + remove() + join() methods performs n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list
Method #4: Using numpy.array() and numpy.char.str_len()
Note: Install numpy module using command “pip install numpy”
Python3
import numpy as np # initialize list test_list = [ 'gfg' , 'is' , 'best' , 'for' , 'geeks' ] # printing original list print ( "The original list : " + str (test_list)) # initialize Threshold thres = 4 # convert list to numpy array test_array = np.array(test_list) # using numpy.char.str_len() to get the length of each string in the array lengths = np.char.str_len(test_array) # using numpy array indexing to filter above threshold strings res = test_array[lengths > = thres] # printing result print ( "The above Threshold size strings are : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
Time Complexity: O(n)
Auxiliary Space: O(n)
In this method, we first convert the input list to a numpy array using numpy.array(). Then we use numpy.char.str_len() to get the length of each string in the array. Finally, we use numpy array indexing to filter the strings with length greater than or equal to the threshold. This method uses numpy’s built-in functions and can be more efficient for large input lists.
Please Login to comment...