Python – Filter rows with Elements as Multiple of K
Given a Matrix, extract rows with elements multiple of K.
Input : test_list = [[5, 10, 15], [4, 8, 12], [100, 15], [5, 10, 23]], K = 4
Output : [[4, 8, 12]]
Explanation : All are multiples of 4.Input : test_list = [[5, 10, 15], [4, 8, 11], [100, 15], [5, 10, 23]], K = 4
Output : []
Explanation : No rows with all multiples of 4.
Method #1 : Using list comprehension + all()
In this, we check for all elements to be multiple using all(), and iteration of rows occur using list comprehension.
Python3
# Python3 code to demonstrate working of # Access element at Kth index in String # Using list comprehension + all() # initializing string list test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 res = [sub for sub in test_list if all (ele % K = = 0 for ele in sub)] # printing result print ( "Rows with K multiples : " + str (res)) |
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]] Rows with K multiples : [[5, 10, 15], [100, 15]]
Time Complexity: O(n) where n is the number of elements in the list “test_list”.
Auxiliary Space: O(1) additional space is not needed
Method #2 : Using filter() + lambda + all()
In this, we perform task of filtering using filter() and lambda function and task of checking for all elements in rows using all().
Python3
# Python3 code to demonstrate working of # Access element at Kth index in String # Using filter() + lambda + all() # initializing string list test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # using all() to check for all elements being multiples of K res = list ( filter ( lambda sub : all (ele % K = = 0 for ele in sub), test_list)) # printing result print ( "Rows with K multiples : " + str (res)) |
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]] Rows with K multiples : [[5, 10, 15], [100, 15]]
Method #3:Using itertools.filterfalse() method
Python3
# Python3 code to demonstrate working of # Access element at Kth index in String import itertools # initializing string list test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing K K = 5 # using all() to check for all elements being multiples of K res = list (itertools.filterfalse( lambda sub : not all (ele % K = = 0 for ele in sub), test_list)) # printing result print ( "Rows with K multiples : " + str (res)) |
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]] Rows with K multiples : [[5, 10, 15], [100, 15]]
Time Complexity: O(N*N)
Auxiliary Space: O(N*N)
Method #4:Using the reduce() function and a lambda function with the all() function:
Algorithm :
- Import the reduce() function from the functools module and define the test_list and K variables.
- Use a lambda function with the filter() function to iterate through each sublist of test_list and check if all its elements are divisible by K.
- Print the result.
Python3
from functools import reduce # initializing string list test_list = [[ 5 , 10 , 15 ], [ 4 , 8 , 3 ], [ 100 , 15 ], [ 5 , 10 , 23 ]] # printing original list print ( "The original list is : " + str (test_list)) K = 5 res = list ( filter ( lambda x: reduce ( lambda a, b: a and b, [i % K = = 0 for i in x]), test_list)) # printing result print ( "Rows with K multiples : " + str (res)) #This code is contributed by Jyothi pinjala. |
The original list is : [[5, 10, 15], [4, 8, 3], [100, 15], [5, 10, 23]] Rows with K multiples : [[5, 10, 15], [100, 15]]
The time complexity : O(n*m), where n is the number of sublists in test_list and m is the maximum length of a sublist. This is because the filter() function and the lambda function inside it are applied to each sublist.
The space complexity : O(k), where k is the number of sublists that satisfy the condition of having all their elements divisible by K. This is because a new list is created to store the filtered sublists, and the maximum size of this list is k.
Please Login to comment...