Python Program to Extract Rows of a matrix with Even frequency Elements
Given a Matrix, the task is to write a Python program to extract all the rows which have even frequencies of elements.
Examples:
Input: [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Output: [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Explanation: frequency of 4-> 4 which is even frequency of 2-> 2 which is even frequency of 6-> 2 which is even frequency of 5-> 2 which is even
Method 1 : Using list comprehension, Counter() and all()
In this, count is maintained using Counter(), and all() is used to check if all frequencies are even, if not, row is not entered in result list.
Program:
Python3
from collections import Counter # initializing list test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] # printing original list print ( "The original list is : " + str (test_list)) # Counter() gets the required frequency res = [sub for sub in test_list if all ( val % 2 = = 0 for key, val in list ( dict (Counter(sub)).items()))] # printing result print ( "Filtered Matrix ? : " + str (res)) |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Time Complexity: O(M*N)
Auxiliary Space: O(K)
Similar to the above method, the difference being filter() and lambda function is used for the task of filtering even frequency rows.
Program:
Python3
from collections import Counter # initializing list test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] # Printing original list print ( "The original list is : " + str (test_list)) # Counter() gets the required frequency # filter() used to perform filtering res = list ( filter ( lambda sub: all (val % 2 = = 0 for key, val in list ( dict (Counter(sub)).items())), test_list)) # Printing the result print ( "Filtered Matrix ? : " + str (res)) |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Time Complexity: O(m*n), where m and n is the number of rows and columns in the list “test_list”.
Auxiliary Space: O(k), where k is the number of elements
Method 3 : Using set() and count() methods
Python3
# Python Program to Extract Rows of a matrix with Even frequency Elements # initializing list test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] # printing original list print ( "The original list is : " + str (test_list)) res = [] for i in test_list: x = set (i) c = 0 for j in x: if (i.count(j) % 2 = = 0 ): c + = 1 if (c = = len (x)): res.append(i) # printing result print ( "Filtered Matrix ? : " + str (res)) |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Method 4 : Using set() and operator.countOf() methods
Python3
# Python Program to Extract Rows of a matrix with Even frequency Elements # initializing list import operator as op test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] # printing original list print ( "The original list is : " + str (test_list)) res = [] for i in test_list: x = set (i) c = 0 for j in x: if (op.countOf(i,j) % 2 = = 0 ): c + = 1 if (c = = len (x)): res.append(i) # printing result print ( "Filtered Matrix ? : " + str (res)) |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Time Complexity: O(n*n )
Auxiliary Space: O(n*n)
Method 5: Using nested for loop +all()
Python3
test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] print ( "The original list is : " + str (test_list)) result = [] for sub in test_list: count = dict () for ele in sub: if ele % 2 = = 0 : count[ele] = count.get(ele, 0 ) + 1 if all (val % 2 = = 0 for val in count.values()): result.append(sub) print ( "Filtered Matrix ? : " + str (result)) #This code is contributed by Jyothi pinjala. |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Time Complexity: O(n*n)
Auxiliary Space: O(n)
Method 6: Using only built-in Python functions
Python3
test_list = [[ 4 , 5 , 5 , 2 ], [ 4 , 4 , 4 , 4 , 2 , 2 ], [ 6 , 5 , 6 , 5 ], [ 1 , 2 , 3 , 4 ]] # printing original list print ( "The original list is : " + str (test_list)) res = [] for row in test_list: counts = {} for elem in row: counts[elem] = counts.get(elem, 0 ) + 1 if all (count % 2 = = 0 for count in counts.values()): res.append(row) # printing result print ( "Filtered Matrix ? : " + str (res)) |
The original list is : [[4, 5, 5, 2], [4, 4, 4, 4, 2, 2], [6, 5, 6, 5], [1, 2, 3, 4]] Filtered Matrix ? : [[4, 4, 4, 4, 2, 2], [6, 5, 6, 5]]
Time complexity: O(n^2) where n is the size of the input list of lists.
Auxiliary space: O(n) to store the resulting list of lists.
Contact Us