Python Program to Extract Rows of a matrix with Even frequency Elements
Last Updated :
04 May, 2023
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
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))
res = [sub for sub in test_list if all (
val % 2 = = 0 for key, val in list ( dict (Counter(sub)).items()))]
print ( "Filtered Matrix ? : " + str (res))
|
Output
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)
Method 2: Using filter(), Counter() and items()
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
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))
res = list ( filter ( lambda sub: all (val % 2 = = 0 for key,
val in list ( dict (Counter(sub)).items())), test_list))
print ( "Filtered Matrix ? : " + str (res))
|
Output
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
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))
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)
print ( "Filtered Matrix ? : " + str (res))
|
Output
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
import operator as op
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))
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)
print ( "Filtered Matrix ? : " + str (res))
|
Output
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))
|
Output
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 ]]
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)
print ( "Filtered Matrix ? : " + str (res))
|
Output
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.
Similar Reads
Python program to print Rows where all its Elements' frequency is greater than K
Given Matrix, extract all rows whose all elements have a frequency greater than K. Input : test_list = [[1, 1, 2, 3, 2, 3], [4, 4, 5, 6, 6], [1, 1, 1, 1], [4, 5, 6, 8]], K = 2 Output : [[1, 1, 1, 1]] Explanation : Here, frequency of 1 is 4 > 2, hence row retained. Input : test_list = [[1, 1, 2, 3
8 min read
Python program to remove rows with duplicate element in Matrix
Given Matrix, remove all rows which have duplicate elements in them. Input : test_list = [[4, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [[4, 3, 2]] Explanation : [4, 3, 2] is the only unique row. Input : test_list = [[4, 3, 3, 2], [7, 6, 7], [2, 4, 4], [8, 9, 9]] Output : [] Explanation : No
5 min read
Python program to extract rows with common difference elements
Given a Matrix, extract rows with AP sequence. Input : test_list = [[4, 7, 10], [8, 10, 12], [10, 11, 13], [6, 8, 10]] Output : [[4, 7, 10], [8, 10, 12], [6, 8, 10]] Explanation : 3, 4, and 2 are common difference in AP. Input : test_list = [[4, 7, 10], [8, 10, 13], [10, 11, 13], [6, 8, 10]] Output
3 min read
Python program to extract rows from Matrix that has distinct data types
Given a Matrix, the task is to write a Python program to extract rows with no repeated data types. Examples: Input : test_list = [[4, 3, 1], ["gfg", 3, {4:2}], [3, 1, "jkl"], [9, (2, 3)]]Output : [['gfg', 3, {4: 2}], [9, (2, 3)]]Explanation : [4, 3, 1] are all integers hence omitted. [9, (2, 3)] has
6 min read
Python Program that prints the rows of a given length from a matrix
Given a Matrix, the following articles shows how to extract all the rows with a specified length. Input : test_list = [[3, 4, 5, 6], [1, 4, 6], [2], [2, 3, 4, 5, 6], [7, 3, 1]], K = 3 Output : [[1, 4, 6], [7, 3, 1]] Explanation : Extracted lists have length of 3.Input : test_list = [[3, 4, 5, 6], [1
4 min read
Python Program for Frequencies of even and odd numbers in a matrix
Given a matrix of order m*n then the task is to find the frequency of even and odd numbers in matrix Examples: Input : m = 3, n = 3 { 1, 2, 3 }, { 4, 5, 6 }, { 7, 8, 9 } Output : Frequency of odd number = 5 Frequency of even number = 4 Input : m = 3, n = 3 { 10, 11, 12 }, { 13, 14, 15 }, { 16, 17, 1
4 min read
Python Program to Remove First Diagonal Elements from a Square Matrix
Given a square matrix of N*N dimension, the task is to write a Python program to remove the first diagonal. Examples: Input : test_list = [[5, 3, 3, 2, 1], [5, 6, 7, 8, 2], [9, 3, 4, 6, 7], [0, 1, 2, 3, 5], [2, 5, 4, 3, 5]] Output : [[3, 3, 2, 1], [5, 7, 8, 2], [9, 3, 6, 7], [0, 1, 2, 5], [2, 5, 4,
6 min read
Python Program that filters out non-empty rows of a matrix
Given Matrix, the following article shows how to filter all the Non-Empty rows of a matrix. In simpler terms, the codes provided below return a matrix after removing empty rows from it. Input : test_list = [[4, 5, 6, 7], [], [], [9, 8, 1], []] Output : [[4, 5, 6, 7], [9, 8, 1]] Explanation : All emp
4 min read
Python program to Convert a Matrix to Sparse Matrix
Converting a matrix to a sparse matrix involves storing only non-zero elements along with their row and column indices to save memory. Using a DictionaryConverting a matrix to a sparse matrix using a dictionary involves storing only the non-zero elements of the matrix, with their row and column indi
2 min read
Python Program to convert a list into matrix with size of each row increasing by a number
Given a list and a number N, the task here is to write a python program to convert it to matrix where each row has N elements more than previous row elements from list. Input : test_list = [4, 6, 8, 1, 2, 9, 0, 10, 12, 3, 9, 1], N = 3 Output : [[4, 6, 8], [4, 6, 8, 1, 2, 9], [4, 6, 8, 1, 2, 9, 0, 10
6 min read