Python – Extract Particular data type rows
Given A Matrix, extract all the rows which have all the elements with particular data type.
Input : test_list = [[4, 5, “Hello”], [2, 6, 7], [“g”, “f”, “g”], [9, 10, 11]], data_type = int
Output : [[2, 6, 7], [9, 10, 11]]
Explanation : All lists with integer are extracted.Input : test_list = [[4, 5, “Hello”], [2, 6, 7], [“g”, “f”, “g”], [9, 10, 11]], data_type = str
Output : [[“g”, “f”, “g”]]
Explanation : All lists with strings are extracted.
Method #1 : Using isinstance() + all() + list comprehension
In this, we check for data type using isinstance(), all() checks for all the elements being of particular data type.
Python3
# Python3 code to demonstrate working of # Extract Particular data type rows # Using isinstance() + all() + list comprehension # initializing list test_list = [[ 4 , 5 , "Hello" ], [ 2 , 6 , 7 ], [ "g" , "f" , "g" ], [ 9 , 10 , 11 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing data type data_type = int # checking data type using isinstance res = [row for row in test_list if all ( isinstance (ele, data_type) for ele in row)] # printing result print ( "Filtered Rows : " + str (res)) |
Output:
The original list is : [[4, 5, ‘Hello’], [2, 6, 7], [‘g’, ‘f’, ‘g’], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]
Time Complexity: O(n*n)
Auxiliary Space: O(1)
Method #2 : Using filter() + lambda + isinstance()
In this, we perform task of filtering using filter() and lambda, isinstance(), is used to perform task of type check as in case of above method.
Python3
# Python3 code to demonstrate working of # Extract Particular data type rows # Using filter() + lambda + isinstance() # initializing list test_list = [[ 4 , 5 , "Hello" ], [ 2 , 6 , 7 ], [ "g" , "f" , "g" ], [ 9 , 10 , 11 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing data type data_type = int # checking data type using isinstance # filter() used to get filter res = list ( filter ( lambda row: all ( isinstance (ele, data_type) for ele in row), test_list)) # printing result print ( "Filtered Rows : " + str (res)) |
Output:
The original list is : [[4, 5, ‘Hello’], [2, 6, 7], [‘g’, ‘f’, ‘g’], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]
Time Complexity: O(n), where n is the length of the input list. This is because we’re using the built-in filter() + lambda + isinstance() which has a time complexity of O(n) in the worst case.
Auxiliary Space: O(n), where n is the length of the input list as we’re using additional space other than the input list itself.
Method #3: Using type().type() returns the data type of variable.
Python3
# Python3 code to demonstrate working of # Extract Particular data type rows # initializing list test_list = [[ 4 , 5 , "Hello" ], [ 2 , 6 , 7 ], [ "g" , "f" , "g" ], [ 9 , 10 , 11 ]] # printing original list print ( "The original list is : " + str (test_list)) # initializing data type data_type = int res = [] # checking data type for i in test_list: c = 0 for j in i: if ( type (j) is data_type): c + = 1 if (c = = len (i)): res.append(i) # printing result print ( "Filtered Rows : " + str (res)) |
The original list is : [[4, 5, 'Hello'], [2, 6, 7], ['g', 'f', 'g'], [9, 10, 11]] Filtered Rows : [[2, 6, 7], [9, 10, 11]]
Please Login to comment...