Open In App

Python – List Strings frequency in Matrix

Improve
Improve
Like Article
Like
Save
Share
Report

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




# Python3 code to demonstrate
# List Strings frequency in Matrix
# using count() + loop
 
# Initializing lists
test_list1 = [['Gfg', 'is', 'best'], ['Gfg', 'is',
                                      'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
test_list2 = ['Gfg', 'is', 'best']
 
# printing original list1
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# List Strings frequency in Matrix
# using count() + loop
res = []
for val in test_list1:
    res.append([val.count(ele) for ele in test_list2])
 
# printing result
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




# Python3 code to demonstrate
# List Strings frequency in Matrix
# using list comprehension
 
# Initializing lists
test_list1 = [['Gfg', 'is', 'best'], ['Gfg', 'is',
              'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
test_list2 = ['Gfg', 'is', 'best']
 
# printing original list1
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# List Strings frequency in Matrix
# using list comprehension
res = [[sub.count(ele) for ele in test_list2] for sub in test_list1]
 
# printing result
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




# Python3 code to demonstrate
# List Strings frequency in Matrix
# using operator.countOf() + loop
import operator as op
 
# Initializing lists
test_list1 = [['Gfg', 'is', 'best'], ['Gfg', 'is',
    'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
test_list2 = ['Gfg', 'is', 'best']
 
# printing original list1
print("The original list 1 is : " + str(test_list1))
print("The original list 2 is : " + str(test_list2))
 
# List Strings frequency in Matrix
# using operator.countOf() + loop
res = []
for val in test_list1:
    res.append([op.countOf(val, ele) for ele in test_list2])
 
# printing result
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:

  1. Initialize an empty dictionary to store the frequency of each string in the matrix.
  2. Loop through each sublist in the matrix.
  3. Loop through each string in the sublist.
  4. Check if the string exists in the dictionary.
  5. If the string exists in the dictionary, increment its count by 1. Otherwise, add the string to the dictionary with a count of 1.
  6. Return the dictionary.

Python3




# Initializing lists
test_list1 = [['Gfg', 'is', 'best'], ['Gfg', 'is',
                                      'for', 'CS'], ['Gfg', 'is', 'for', 'Geeks']]
test_list2 = ['Gfg', 'is', 'best']
 
# Define a function to count the frequency of strings in the matrix
 
 
def count_strings_freq(matrix, strings):
   
   
    res = []
     
    for sublist in matrix:
       
        # initial frequency is set to null
        freq_list = []
        for string in strings:
            freq_list.append(sublist.count(string))
             
        # Appending into result    
        res.append(freq_list)
     
    return res
 
 
# Call the function with the input lists
res = count_strings_freq(test_list1, test_list2)
 
# printing result
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
Previous
Next
Share your thoughts in the comments
Similar Reads