Given a matrix, the task here is to write a Python program to remove rows that have any element from the custom list and then display the result.
Examples:
Input : test_list = [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]], check_list = [3, 10, 19, 29, 20, 15]
Output : [[7, 8, 9], [12, 18, 21]]
Explanation : [5, 3, 1] row has 3 in it in custom list, hence omitted.
Input : test_list = [[5, 3, 1], [7, 8, 19], [1, 10, 22], [12, 18, 20]], check_list = [3, 10, 19, 29, 20, 15]
Output : []
Explanation : All rows have some element from custom list, hence omitted.
Method 1: Using any() and list comprehension
In this, we perform the task of checking for any elements from custom list to check for rows using any() and list comprehension is used to omit row if any element from custom list is found in row.
Example:
Python3
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
res = [row for row in test_list if not any (el in row for el in check_list)]
print ( "The omitted rows matrix : " + str (res))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method 2 : Using filter(), lambda and any()
Similar to above method, only difference being filter() and lambda function is used to perform task of filtering out or omit rows from matrix from result.
Example:
Python3
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
res = list ( filter ( lambda row: not any (
el in row for el in check_list), test_list))
print ( "The omitted rows matrix : " + str (res))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method 3 : Using Counter() function
Python3
from collections import Counter
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
freq = Counter(check_list)
for i in test_list.copy():
for j in i:
if j in freq.keys():
test_list.remove(i)
break
print ( "The omitted rows matrix : " + str (test_list))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method #4:Using operator.countOf() method
Python3
from collections import Counter
import operator as op
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
freq = Counter(check_list)
for i in test_list.copy():
for j in i:
if op.countOf(freq.keys(), j) > 0 :
test_list.remove(i)
break
print ( "The omitted rows matrix : " + str (test_list))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method #5: Using for loop:
Python3
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
res = []
for row in test_list:
if not any (el in row for el in check_list):
res.append(row)
print ( "The omitted rows matrix : " + str (res))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
Time Complexity: O(N2)
Auxiliary Space: O(N2)
Method#6: Using Recursive method.
Here, the function omit_rows_recursive takes a matrix matrix and a list check_list as input. The base case is when the matrix is empty, in which case an empty list is returned. If the first row of the matrix contains any elements from the check list, the function recursively calls itself with the remaining part of the matrix after the first row. Otherwise, the function concatenates the first row with the result of recursively calling itself with the remaining part of the matrix after the first row.
Python3
def omit_rows_recursive(matrix, check_list):
if not matrix:
return []
if any (el in matrix[ 0 ] for el in check_list):
return omit_rows_recursive(matrix[ 1 :], check_list)
else :
return [matrix[ 0 ]] + omit_rows_recursive(matrix[ 1 :], check_list)
test_list = [[ 5 , 3 , 1 ], [ 7 , 8 , 9 ], [ 1 , 10 , 22 ], [ 12 , 18 , 21 ]]
print ( "The original list is : " + str (test_list))
check_list = [ 3 , 10 , 19 , 29 , 20 , 15 ]
res = omit_rows_recursive(test_list,check_list)
print ( "The omitted rows matrix : " + str (res))
|
Output
The original list is : [[5, 3, 1], [7, 8, 9], [1, 10, 22], [12, 18, 21]]
The omitted rows matrix : [[7, 8, 9], [12, 18, 21]]
The time complexity of the recursive function is also O(nm), as it must examine each element of each row in the matrix once.
The auxiliary space of the recursive function is also O(nm), as it may need to store up to n rows of m elements each in the result. However, the recursive function may have additional space complexity due to the function call stack, depending on the implementation.
Last Updated :
25 Mar, 2023
Like Article
Save Article
Share your thoughts in the comments
Please Login to comment...