Given a Matrix, the task is to write a Python program to extract all the rows which have paired elements. i.e elements have a frequency of mod 2.
Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2]]
Output : [[5, 5, 4, 7, 7, 4], [1, 1, 2, 2]]
Explanation : All rows have pair elements, i.e have even occurrence.
Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 4], [1, 2], [1, 1, 2, 2]]
Output : [[1, 1, 2, 2]]
Explanation : All rows have pair elements, i.e have even occurrence.
Method #1 : Using all() + list comprehension + count()
In this, we check for the count of each element using count(), and all() is used to test for all elements’ frequency is divisible by 2.
Python3
# Python3 code to demonstrate working of # Extract Paired Rows # Using all() + list comprehension + count() # initializing list test_list = [[ 10 , 2 , 3 , 2 , 3 ], [ 5 , 5 , 4 , 7 , 7 , 4 ], [ 1 , 2 ], [ 1 , 1 , 2 , 2 ]] # printing original list print ( "The original list is : " + str (test_list)) # count() checks for frequency to be mod 2 res = [row for row in test_list if all ( row.count(ele) % 2 = = 0 for ele in row)] # printing result print ( "Extracted rows : " + str (res)) |
Output:
The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2]] Extracted rows : [[5, 5, 4, 7, 7, 4], [1, 1, 2, 2]]
Time Complexity: O(n)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + count() + all()
In this, we perform the task of filtering using filter() and lambda function instead of list comprehension. The count() and all() are used to check for the frequency of all the elements in rows.
Python3
# Python3 code to demonstrate working of # Extract Paired Rows # Using filter() + lambda + count() + all() # initializing list test_list = [[ 10 , 2 , 3 , 2 , 3 ], [ 5 , 5 , 4 , 7 , 7 , 4 ], [ 1 , 2 ], [ 1 , 1 , 2 , 2 ]] # printing original list print ( "The original list is : " + str (test_list)) # count() checks for frequency to be mod 2 # filter() and lambda used to perform filtering res = list ( filter ( lambda row: all ( row.count(ele) % 2 = = 0 for ele in row), test_list)) # printing result print ( "Extracted rows : " + str (res)) |
Output:
The original list is : [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2]] Extracted rows : [[5, 5, 4, 7, 7, 4], [1, 1, 2, 2]]
Time Complexity: O(n*n), where n is the number of elements in the list “test_list”.
Auxiliary Space: O(n), where n is the number of elements in the list “test_list”.
Method 3 : using a for loop
step by step approach:
Initialize an empty list to store the paired rows.
Iterate over each row of the input list using a for loop.
Initialize an empty dictionary to count the occurrences of each element in the current row.
Iterate over each element of the current row using a nested for loop.
If the current element is not in the dictionary, add it with a count of 1. Otherwise, increment the count of the current element in the dictionary.
Check whether the frequency of each element in the dictionary is even using a for loop.
If all frequencies are even, append the current row to the list of paired rows.
Return the list of paired rows.
Python3
def extract_paired_rows(test_list): paired_rows = [] for row in test_list: freq = {} for ele in row: if ele not in freq: freq[ele] = 1 else : freq[ele] + = 1 if all (freq[ele] % 2 = = 0 for ele in freq): paired_rows.append(row) return paired_rows test_list = [[ 10 , 2 , 3 , 2 , 3 ], [ 5 , 5 , 4 , 7 , 7 , 4 ], [ 1 , 2 ], [ 1 , 1 , 2 , 2 ]] print ( "The original list is:" , test_list) paired_rows = extract_paired_rows(test_list) print ( "Extracted rows:" , paired_rows) |
The original list is: [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2]] Extracted rows: [[5, 5, 4, 7, 7, 4], [1, 1, 2, 2]]
Time complexity: O(nm), where n is the number of rows and m is the maximum length of a row.
Auxiliary space: O(m), for the dictionary to count the occurrences of each element