Python – List Strings frequency in Matrix
Sometimes, while working with Matrix, we can have a problem in which we need to check the frequency of argument Strings from List in each row of Matrix. This is a very peculiar problem and can have usefulness in many domains. Let us discuss certain ways in which this task can be solved.
Method #1 : Using count() + loop
The combination of the above functionalities can be used to perform this task. In this we count the frequency using count() and the task of iteration is performed inside the loop.
Python3
test_list1 = [[ 'Gfg' , 'is' , 'best' ], [ 'Gfg' , 'is' ,
'for' , 'CS' ], [ 'Gfg' , 'is' , 'for' , 'Geeks' ]]
test_list2 = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = []
for val in test_list1:
res.append([val.count(ele) for ele in test_list2])
print ( "Frequency of strings in Matrix : " + str (res))
|
Output :
The original list 1 is : [['Gfg', 'is', 'best'], ['Gfg', 'is', 'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
The original list 2 is : ['Gfg', 'is', 'best']
Frequency of strings in Matrix : [[1, 1, 1], [1, 1, 0], [1, 1, 0]]
Time complexity: O(m*n), because it performs the same number of iterations as the original code.
Auxiliary space: O(m*n) as well, because it creates a list with m * n keys and a list of m * n elements
Method #2: Using list comprehension
This is yet another way in which this task can be performed. This is shortened version of the above methodone-lineriner.
Python3
test_list1 = [[ 'Gfg' , 'is' , 'best' ], [ 'Gfg' , 'is' ,
'for' , 'CS' ], [ 'Gfg' , 'is' , 'for' , 'Geeks' ]]
test_list2 = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = [[sub.count(ele) for ele in test_list2] for sub in test_list1]
print ( "Frequency of strings in Matrix : " + str (res))
|
Output :
The original list 1 is : [['Gfg', 'is', 'best'], ['Gfg', 'is', 'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
The original list 2 is : ['Gfg', 'is', 'best']
Frequency of strings in Matrix : [[1, 1, 1], [1, 1, 0], [1, 1, 0]]
Method #3: Using operator.countOf() + loop
The combination of the above functionalities can be used to perform this task. In this we count the frequency using count() and task of iteration is performed inside the loop.
Python3
import operator as op
test_list1 = [[ 'Gfg' , 'is' , 'best' ], [ 'Gfg' , 'is' ,
'for' , 'CS' ], [ 'Gfg' , 'is' , 'for' , 'Geeks' ]]
test_list2 = [ 'Gfg' , 'is' , 'best' ]
print ( "The original list 1 is : " + str (test_list1))
print ( "The original list 2 is : " + str (test_list2))
res = []
for val in test_list1:
res.append([op.countOf(val, ele) for ele in test_list2])
print ( "Frequency of strings in Matrix : " + str (res))
|
Output
The original list 1 is : [['Gfg', 'is', 'best'], ['Gfg', 'is', 'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
The original list 2 is : ['Gfg', 'is', 'best']
Frequency of strings in Matrix : [[1, 1, 1], [1, 1, 0], [1, 1, 0]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method 4: Using a dictionary
Approach:
- Initialize an empty dictionary to store the frequency of each string in the matrix.
- Loop through each sublist in the matrix.
- Loop through each string in the sublist.
- Check if the string exists in the dictionary.
- If the string exists in the dictionary, increment its count by 1. Otherwise, add the string to the dictionary with a count of 1.
- Return the dictionary.
Python3
test_list1 = [[ 'Gfg' , 'is' , 'best' ], [ 'Gfg' , 'is' ,
'for' , 'CS' ], [ 'Gfg' , 'is' , 'for' , 'Geeks' ]]
test_list2 = [ 'Gfg' , 'is' , 'best' ]
def count_strings_freq(matrix, strings):
res = []
for sublist in matrix:
freq_list = []
for string in strings:
freq_list.append(sublist.count(string))
res.append(freq_list)
return res
res = count_strings_freq(test_list1, test_list2)
print ( "Frequency of strings in Matrix : " + str (res))
|
Output
Frequency of strings in Matrix : [[1, 1, 1], [1, 1, 0], [1, 1, 0]]
Time complexity: O(n*m), where n is the number of sublists in the matrix and m is the total number of strings in all sublists.
Auxiliary space: O(k), where k is the number of unique strings in the matrix.
Last Updated :
26 Apr, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...