Python – Extract Paired Rows
Last Updated :
26 Apr, 2023
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
test_list = [[ 10 , 2 , 3 , 2 , 3 ], [ 5 , 5 , 4 , 7 , 7 , 4 ],
[ 1 , 2 ], [ 1 , 1 , 2 , 2 ]]
print ( "The original list is : " + str (test_list))
res = [row for row in test_list if all (
row.count(ele) % 2 = = 0 for ele in row)]
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
test_list = [[ 10 , 2 , 3 , 2 , 3 ], [ 5 , 5 , 4 , 7 , 7 , 4 ],
[ 1 , 2 ], [ 1 , 1 , 2 , 2 ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda row: all (
row.count(ele) % 2 = = 0 for ele in row), test_list))
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)
|
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(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
Like Article
Suggest improvement
Share your thoughts in the comments
Please Login to comment...