Open In App

Python – Test if Rows have Similar frequency

In this article we have a given Matrix, test if all rows have similar elements.

Input : test_list = [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]] 
Output : True 
Explanation : All lists have 2, 3, 4, 6, 7.



Input : test_list = [[6, 4, 2, 7, 3], [7, 5, 6, 4, 2], [2, 4, 7, 3, 6]] 
Output : False 
Explanation : 2nd list has 5 instead of 3. 

Method #1 : Using Counter() + list comprehension



In this, we compute the elements’ frequency dictionary using Counter(), and compare with each row in Matrix, if they check out then True is returned.




# Python3 code to demonstrate working of
# Test if Rows have Similar frequency
# Using Counter() + list comprehension
from collections import Counter
 
# initializing list
test_list = [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking if all rows are similar
res = all(dict(Counter(row)) == dict(Counter(test_list[0])) for row in test_list)
 
# printing result
print("Are all rows similar : " + str(res))

Output
The original list is : [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
Are all rows similar : True

Time Complexity: O(n*m)
Auxiliary Space: O(1)

Method #2 : Using list comprehension + sorted() + all()

In this, we check for similar elements using sorted(), by ordering all the elements to sorted format.




# Python3 code to demonstrate working of
# Test if Rows have Similar frequency
# Using list comprehension + sorted() + all()
 
# initializing list
test_list = [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking if all rows are similar
# ordering each row to test
res = all(list(sorted(row)) == list(sorted(test_list[0])) for row in test_list)
 
# printing result
print("Are all rows similar : " + str(res))

Output
The original list is : [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
Are all rows similar : True

Time Complexity: O(nlogn) where n is the number of elements in the list “test_list”.  The time complexity of the sorted() function is O(n log n)
Auxiliary Space: O(1), no extra space is required

Method #3 : Using for loops + extend(),set(),count() methods




# Python3 code to demonstrate working of
# Test if Rows have Similar frequency
 
# initializing list
test_list = [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking if all rows are similar
res=[]
x=[]
for i in test_list:
    x.extend(i)
a=list(set(x))
y=[]
for i in a:
    y.append(x.count(i))
res=len(set(y))==1
# printing result
print("Are all rows similar : " + str(res))

Output
The original list is : [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
Are all rows similar : True

Time Complexity : O(N*N)
Auxiliary Space : O(N)

Method#4: Using dictionary comprehension

This Python code  checks if all the rows in a 2D list have similar frequency of elements. The input is a list of lists test_list, where each inner list represents a row. The code uses a dictionary comprehension to count the frequency of each element in each row, and then compares these dictionaries with the dictionary of the first row to check if they are equal.




# Python3 code to demonstrate working of
# Test if Rows have Similar frequency
# Using dictionary comprehension
 
# initializing list
test_list = [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# checking if all rows are similar
res = all({i: row.count(i) for i in row} == {i: test_list[0].count(i) for i in test_list[0]} for row in test_list)
 
# printing result
print("Are all rows similar : " + str(res))
#This code is contributed by Vinay Pinjala.

Output
The original list is : [[6, 4, 2, 7, 3], [7, 3, 6, 4, 2], [2, 4, 7, 3, 6]]
Are all rows similar : True

Time Complexity : O(N*N)
Auxiliary Space : O(N)


Article Tags :