Python - Test if Rows have Similar frequency
Last Updated :
08 Mar, 2023
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
# 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))
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
# 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))
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
# 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))
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
# 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.
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)
Similar Reads
Python - Sort rows by Frequency of K Given a Matrix, the task is to write a Python program to perform sorting on rows depending on the frequency of K. Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], K = 2 Output : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]] Explanation : 0 < 1
4 min read
Python | Test list element similarity Given a list, your task is to determine the list is K percent same i.e has element that is populated more than K % times. Given below are few methods to solve the task. Method #1 Using collections.Counter Python3 # Python3 code to demonstrate # to check whether the list # K percent same or not from
4 min read
Python - Remove Similar Rows from Tuple Matrix Sometimes, while working with Tuple Matrix, we can have a problem in which we get lots of data, which are similar, i.e elements are same in rows, just the ordering of tuples is different, it's sometimes, desired to get them removed. This kind of problem can have application in domains such as web de
9 min read
Python | Record Similar tuple occurrences Sometimes, while working with data, we can have a problem in which we need to check the occurrences of records that occur at similar times. This has applications in the web development domain. Let's discuss certain ways in which this task can be performed. Method #1 : Using map() + Counter() + sorte
9 min read
Python - Similar index elements frequency Sometimes, while working with Python list, we can have a problem in which we need to check if one element has similar index occurrence in other list. This can have possible application in many domains. Lets discuss certain ways in which this task can be performed. Method #1 : Using sum() + zip() The
7 min read
Python - Count the frequency of matrix row length 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 :
5 min read