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
from collections import Counter
test_list = [[ 6 , 4 , 2 , 7 , 3 ], [ 7 , 3 , 6 , 4 , 2 ], [ 2 , 4 , 7 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
res = all ( dict (Counter(row)) = = dict (Counter(test_list[ 0 ])) for row in test_list)
print ( "Are all rows similar : " + str (res))
|
OutputThe 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
test_list = [[ 6 , 4 , 2 , 7 , 3 ], [ 7 , 3 , 6 , 4 , 2 ], [ 2 , 4 , 7 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
res = all ( list ( sorted (row)) = = list ( sorted (test_list[ 0 ])) for row in test_list)
print ( "Are all rows similar : " + str (res))
|
OutputThe 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
test_list = [[ 6 , 4 , 2 , 7 , 3 ], [ 7 , 3 , 6 , 4 , 2 ], [ 2 , 4 , 7 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
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
print ( "Are all rows similar : " + str (res))
|
OutputThe 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
test_list = [[ 6 , 4 , 2 , 7 , 3 ], [ 7 , 3 , 6 , 4 , 2 ], [ 2 , 4 , 7 , 3 , 6 ]]
print ( "The original list is : " + str (test_list))
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)
print ( "Are all rows similar : " + str (res))
|
OutputThe 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)