Python – Custom Rows Removal depending on Kth Column
Sometimes, while working with Python Matrix, we can have a problem in which we need to remove elements from Matrix depending on its Kth Column element present in argument list. This can have application in many domains. Lets discuss certain ways in which this task can be performed.
Method #1 : Using loop This is brute way in which this task can be performed. In this, we iterate rows of matrix and check for Kth column matching value from list and exclude from new list.
Python3
# Python3 code to demonstrate # Custom Rows Removal depending on Kth Column # using loop # Initializing lists test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]] test_list2 = [ 12 , 4 , 6 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Initializing K K = 1 # Custom Rows Removal depending on Kth Column # using loop res = [] for ele in test_list1: if ele[K] not in test_list2: res.append(ele) # printing result print ("The matrix after rows removal is : " + str (res)) |
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]] The original list 2 is : [12, 4, 6] The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Method #2 : Using list comprehension This is yet another way in which this task can be performed. In this, we perform similar task in shortened format using list comprehension in one line.
Python3
# Python3 code to demonstrate # Custom Rows Removal depending on Kth Column # using list comprehension # Initializing lists test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]] test_list2 = [ 12 , 4 , 6 ] # printing original lists print ("The original list 1 is : " + str (test_list1)) print ("The original list 2 is : " + str (test_list2)) # Initializing K K = 1 # Custom Rows Removal depending on Kth Column # using list comprehension res = [ele for ele in test_list1 if ele[K] not in test_list2] # printing result print ("The matrix after rows removal is : " + str (res)) |
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]] The original list 2 is : [12, 4, 6] The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Method #3 : Here’s an approach using the filter function:
Python3
# Python3 code to demonstrate # Custom Rows Removal depending on Kth Column # using filter function # Initializing lists test_list1 = [[ 3 , 4 , 5 ], [ 2 , 6 , 8 ], [ 1 , 10 , 2 ], [ 5 , 7 , 9 ], [ 10 , 1 , 2 ]] test_list2 = [ 12 , 4 , 6 ] # printing original lists print ( "The original list 1 is : " + str (test_list1)) print ( "The original list 2 is : " + str (test_list2)) # Initializing K K = 1 # Custom Rows Removal depending on Kth Column # using filter function res = list ( filter ( lambda x: x[K] not in test_list2, test_list1)) # printing result print ( "The matrix after rows removal is : " + str (res)) |
The original list 1 is : [[3, 4, 5], [2, 6, 8], [1, 10, 2], [5, 7, 9], [10, 1, 2]] The original list 2 is : [12, 4, 6] The matrix after rows removal is : [[1, 10, 2], [5, 7, 9], [10, 1, 2]]
Time complexity: O(n) where n is the number of elements in test_list1.
Auxiliary Space: O(n) where n is the number of elements in res.
Explanation:
The filter function is applied to test_list1 using a lambda function that checks if the Kth element of each element in test_list1 is not in test_list2.
The result of filter is converted to a list and stored in res.
Please Login to comment...