Python – Prefix frequency in string List
Last Updated :
24 Mar, 2023
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
print ( "The original list is : " + str (test_list))
test_sub = 'gfg'
res = 0
for ele in test_list:
if ele.startswith(test_sub):
res = res + 1
print ( "Strings count with matching frequency : " + str (res))
|
Output
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
print ( "The original list is : " + str (test_list))
test_sub = 'gfg'
res = sum (sub.startswith(test_sub) for sub in test_list)
print ( "Strings count with matching frequency : " + str (res))
|
Output
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
print ( "The original list is : " + str (test_list))
test_sub = 'gfg'
res = 0
for ele in test_list:
if ele.find(test_sub) = = 0 :
res = res + 1
print ( "Strings count with matching frequency : " + str (res))
|
Output
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
print ( "The original list is : " + str (test_list))
test_sub = 'gfg'
res = 0
for ele in test_list:
if re.match(f '^{test_sub}' , ele):
res + = 1
print ( "Strings count with matching frequency : " + str (res))
|
Output
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
test_sub = 'gfg'
count_func = lambda count, string: count + 1 if string.startswith(test_sub) else count
res = reduce (count_func, test_list, 0 )
print ( "The original list is : " + str (test_list))
print ( "Strings count with matching frequency : " + str (res))
|
Output
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
test_list = [ 'gfgisbest' , 'geeks' , 'gfgfreak' , 'gfgCS' , 'Gcourses' ]
test_sub = 'gfg'
new_iter = filter ( lambda element: element.startswith(test_sub), test_list)
count = len ( list (new_iter))
print ( "The original list is : " + str (test_list))
print ( "Strings count with matching frequency: " , count)
|
Output
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...