Python – Count the frequency of matrix row length
Last Updated :
22 Apr, 2023
Given a Matrix, the task is to write a Python program to get the count frequency of its rows lengths.
Input : test_list = [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Output : {3: 2, 2: 2, 1: 1}
Explanation : 2 lists of length 3 are present, 2 lists of size 2 and 1 of 1 length is present.
Input : test_list = [[6, 3, 1], [8, 9], [10, 12, 7], [4, 11]]
Output : {3: 2, 2: 2}
Explanation : 2 lists of length 3 are present, 2 lists of size 2.
Method #1 : Using dictionary + loop
In this we check for each row length, if length has occurred in the memorize dictionary, then the result is incremented or if a new size occurs, the element is registered as new.
Python3
test_list = [[ 6 , 3 , 1 ], [ 8 , 9 ], [ 2 ],
[ 10 , 12 , 7 ], [ 4 , 11 ]]
print ( "The original list is : " + str (test_list))
res = dict ()
for sub in test_list:
if len (sub) not in res:
res[ len (sub)] = 1
else :
res[ len (sub)] + = 1
print ( "Row length frequencies : " + str (res))
|
Output
The original list is : [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Row length frequencies : {3: 2, 2: 2, 1: 1}
Time complexity: O(n), where n is the total number of sublists in the input list.
Auxiliary space complexity: O(n), where n is the total number of sublists in the input list. The dictionary used to store the frequency of each length takes up O(n) space.
Method #2 : Using Counter() + map() + len()
In this, map() and len() get the lengths of each sublist in the matrix, Counter is used to keep the frequency of each of the lengths.
Python3
from collections import Counter
test_list = [[ 6 , 3 , 1 ], [ 8 , 9 ], [ 2 ],
[ 10 , 12 , 7 ], [ 4 , 11 ]]
print ( "The original list is : " + str (test_list))
res = dict (Counter( map ( len , test_list)))
print ( "Row length frequencies : " + str (res))
|
Output:
The original list is : [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Row length frequencies : {3: 2, 2: 2, 1: 1}
Time complexity: O(n), where n is the total number of elements in the input list.
Auxiliary space: O(k), where k is the number of unique row lengths in the input list.
Method #3 : Using count() method
Python3
test_list = [[ 6 , 3 , 1 ], [ 8 , 9 ], [ 2 ],
[ 10 , 12 , 7 ], [ 4 , 11 ]]
print ( "The original list is : " + str (test_list))
res = dict ()
x = []
for sub in test_list:
x.append( len (sub))
y = set (x)
for i in y:
res[i] = x.count(i)
print ( "Row length frequencies : " + str (res))
|
Output
The original list is : [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Row length frequencies : {1: 1, 2: 2, 3: 2}
Time complexity: O(n^2) where n is the length of the input list.
Auxiliary space: O(n) where n is the length of the input list.
Method #4 : Using operator.countOf() method
Python3
import operator as op
test_list = [[ 6 , 3 , 1 ], [ 8 , 9 ], [ 2 ],
[ 10 , 12 , 7 ], [ 4 , 11 ]]
print ( "The original list is : " + str (test_list))
res = dict ()
x = []
for sub in test_list:
x.append( len (sub))
y = set (x)
for i in y:
res[i] = op.countOf(x,i)
print ( "Row length frequencies : " + str (res))
|
Output
The original list is : [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Row length frequencies : {1: 1, 2: 2, 3: 2}
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #6: Using list comprehension and dictionary
- Initialize a list of sublists test_list.
- Print the original list using the print() function.
- Use a list comprehension to create a new list lengths containing the lengths of all sublists in test_list. The len() function is used to get the length of each sublist.
- Use a dictionary comprehension to count the frequency of each length in the lengths list obtained in step 3. The key of each key-value pair in the resulting dictionary is a length, and the value is the number of times that length appears in the lengths list. The count() method is used to count the frequency of each length.
- Store the resulting dictionary in the variable res.
- Print the resulting dictionary using the print() function.
Python3
test_list = [[ 6 , 3 , 1 ], [ 8 , 9 ], [ 2 ],
[ 10 , 12 , 7 ], [ 4 , 11 ]]
print ( "The original list is : " + str (test_list))
lengths = [ len (sub) for sub in test_list]
res = {length: lengths.count(length) for length in lengths}
print ( "Row length frequencies : " + str (res))
|
Output
The original list is : [[6, 3, 1], [8, 9], [2], [10, 12, 7], [4, 11]]
Row length frequencies : {3: 2, 2: 2, 1: 1}
Time complexity: O(n), where n is the length of test_list.
Auxiliary space: O(k), where k is the number of distinct row lengths in test_list.
Please Login to comment...