Python - List Strings frequency in Matrix
Last Updated :
26 Apr, 2023
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))
OutputThe 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
# 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))
OutputFrequency 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.
Similar Reads
Prefix frequency in string List - Python In this article, we will explore various methods to find prefix frequency in string List. The simplest way to do is by using a loop.Using a LoopOne of the simplest ways to calculate the frequency of a prefix in a list of strings is by iterating through each element and checking if the string starts
2 min read
Python - List Words Frequency in String Given a List of Words, Map frequency of each to occurrence in String. Input : test_str = 'geeksforgeeks is best for geeks and best for CS', count_list = ['best', 'geeksforgeeks', 'computer'] Output : [2, 1, 0] Explanation : best has 2 occ., geeksforgeeks 1 and computer is not present in string.Input
4 min read
Python - Frequency of K in sliced String Given a String, find the frequency of certain characters in the index range. Input : test_str = 'geeksforgeeks is best for geeks', i = 3, j = 9, K = 'e' Output : 0 Explanation : No occurrence of 'e' between 4th [s] and 9th element Input : test_str = 'geeksforgeeks is best for geeks', i = 0, j = 9, K
6 min read
Python - Formable Strings Count in Matrix Given strings matrix, the task is to write a Python program to count strings that can be made from letters from the given list. Examples: Input : test_list = [["gfg", "best"], ["all", "love", "gfg"], ["gfg", "is", "good"], ["geeksforgeeks"]], tar_list = ["g", "f", "s", "b", "o", "d", "e", "t"] Outpu
5 min read
Most Frequent Word in Strings List We are given a list of strings we need to find the most frequent words from that particular list. For example, w = ["apple", "banana", "apple", "orange", "banana", "apple"] we need to find most frequent words in list which is 'apple' in this case.Using Counter from collectionsCounter class from the
2 min read