Python – Prefix frequency in string List
Sometimes, while working with Python lists, we can have a problem in which we need to get the count of strings that start with a particular substring. This can have an application in web development and general programming. Let us discuss certain ways in which this task can be performed.
Method #1 : Using loop + startswith()
The combination of the above functions can be used to perform this task. In this, we run a loop for each string in list and employ startswith() to get the strings that start with a particular prefix.
Python3
# Python3 code to demonstrate # Prefix frequency in List # using loop + startswith() # Initializing list test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List # using loop + startswith() res = 0 for ele in test_list: if ele.startswith(test_sub): res = res + 1 # printing result print ( "Strings count with matching frequency : " + str (res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Method #2: Using sum() + startswith()
The combination of above functions can be used to perform this task. In this, we perform the task of counting using sum() and startswith(), is used to perform task of checking of prefix.
Python3
# Python3 code to demonstrate # Prefix frequency in List # using sum() + startswith() # Initializing list test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List # using sum() + startswith() res = sum (sub.startswith(test_sub) for sub in test_list) # printing result print ( "Strings count with matching frequency : " + str (res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time Complexity: O(n), where n is the length of the list test_list
Auxiliary Space: O(1) constant additional space is created
Method #3 : Using find() method
Python3
# Python3 code to demonstrate # Prefix frequency in List # Initializing list test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List # using loop + find() res = 0 for ele in test_list: if ele.find(test_sub) = = 0 : res = res + 1 # printing result print ( "Strings count with matching frequency : " + str (res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Method #4 : Using re
Another approach to solve this problem is using the re (regular expression) module in Python. This can be done using the re.match function to match the regular expression pattern to the start of the string. Here is an example:
Python3
import re # Initializing list test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # printing original list print ( "The original list is : " + str (test_list)) # Initializing substring test_sub = 'gfg' # Prefix frequency in List using re.match res = 0 for ele in test_list: if re.match(f '^{test_sub}' , ele): res + = 1 # printing result print ( "Strings count with matching frequency : " + str (res)) #This code is contributed by Edula Vinay Kumar Reddy |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time complexity: O(n * m), where n is the length of the list and m is the length of the prefix.
Auxiliary Space: O(1)
Method #5: Using reduce() and lambda function:
In this method, we are using the reduce() function to apply a lambda function to each element of the list. The lambda function checks if the element starts with the given substring, and if so, it increments the count by 1 else no change.
Python3
from functools import reduce # Initializing list test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # Initializing substring test_sub = 'gfg' # using lambda function to check if a string starts with the given substring count_func = lambda count, string: count + 1 if string.startswith(test_sub) else count # using reduce() function to apply the lambda function to each element of the list res = reduce (count_func, test_list, 0 ) # printing original list print ( "The original list is : " + str (test_list)) # printing result print ( "Strings count with matching frequency : " + str (res)) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency : 3
Time complexity: O(n * m) where n is the length of the list and m is the length of the prefix.
Auxiliary Space: O(1)
Method #6: Using the filter() method
Step by step Algorithm:
- Initialize a list of strings and a substring to find in the list.
- Use the filter() method to create a new iterator that contains only the elements in the list that start with the substring.
- Use the len() method to count the number of elements in the iterator.
- Print the original list of strings and the number of strings in the list that start with the substring.
Python3
# Initialize a list of strings test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ] # Initialize a substring to find in the list test_sub = 'gfg' # Create a new iterator that contains only the elements in the list that start with the substring new_iter = filter ( lambda element: element.startswith(test_sub), test_list) # Count the number of elements in the iterator using the len() method count = len ( list (new_iter)) # Print the original list of strings print ( "The original list is : " + str (test_list)) # Print the number of strings in the list that start with the substring print ( "Strings count with matching frequency: " , count) |
The original list is : ['gfgisbest', 'geeks', 'gfgfreak', 'gfgCS', 'Gcourses'] Strings count with matching frequency: 3
Time complexity: O(n) where n is the length of the list.
Auxiliary Space: O(k) where k is the number of elements in the list that start with the given substring.
Please Login to comment...