Given a Matrix, remove rows with integer instances.
Input : test_list = [[4, ‘Gfg’, ‘best’], [‘gfg’, 5, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]]
Output : [[‘GFG’, ‘Best’]]
Explanation : All rows with numbers are removed.
Input : test_list = [[4, ‘Gfg’, ‘best’], [‘gfg’, 5, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’, 1]]
Output : []
Explanation : All rows with numbers are removed. No list left.
Method #1 : Using any() + list comprehension
In this, we check for any integer element using any() in any row, and use list comprehension to perform task of iteration.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = [sub for sub in test_list if not any (
isinstance (ele, int ) for ele in sub)]
print ( "The filtered rows : " + str (res))
|
Output:
The original list is : [[4, ‘Gfg’, ‘best’], [‘gfg’, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]] The filtered rows : [[‘gfg’, ‘is’, ‘best’], [‘GFG’, ‘Best’]]
Method #2 : Using filter() + lambda + any()
In this, we perform task of filtering using lambda and filter(), any() is used in similar way as in above method.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = list ( filter ( lambda sub: not any ( isinstance (ele, int )
for ele in sub), test_list))
print ( "The filtered rows : " + str (res))
|
Output:
The original list is : [[4, ‘Gfg’, ‘best’], [‘gfg’, ‘is’, ‘best’], [3, 5], [‘GFG’, ‘Best’]] The filtered rows : [[‘gfg’, ‘is’, ‘best’], [‘GFG’, ‘Best’]]
Method #3 : Using list(),map(),join() and isalpha() methods
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
for i in test_list:
a = list ( map ( str ,i))
a = "".join(a)
if (a.isalpha()):
res.append(i)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Method #4 : Using list(),map(),join(),replace() and len() methods
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
numbers = "0123456789"
for i in test_list:
a = list ( map ( str ,i))
a = "".join(a)
x = a
for j in numbers:
a = a.replace(j,"")
if ( len (x) = = len (a)):
res.append(i)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
The time complexity of this code is O(n*m), where n is the length of the input list, and m is the length of the longest row in the list.
The auxiliary space complexity of this code is O(n), where n is the length of the input list.
Method#5:Using a loop and isnumeric()
Algorithm:
- Initialize an empty list “res”
- Loop through each row of the input list “test_list”
- Convert each element of the row to a string using the “map” function and the “str” method
- Concatenate the string elements of the row using the “join” method to create a single string “a”
- Check if the string “a” contains only alphabetic characters using the “isalpha” method
- If the string contains only alphabetic characters, append the row to the “res” list
- Return the filtered rows list “res”
- Print the filtered rows list
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [ 'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
print ( "The original list is : " + str (test_list))
res = []
for sublist in test_list:
flag = True
for ele in sublist:
if isinstance (ele, int ) or ele.isnumeric():
flag = False
break
if flag:
res.append(sublist)
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m) where n is the number of rows and m is the number of columns in the input list “test_list”
Auxiliary Space: O(1) (ignoring the space required to store the filtered rows list)
Method 6: using the set() function and set operations.
Here are the steps:
- Define the input list test_list.
- Convert each sublist in test_list into a set using the set() function. This will remove duplicates and convert all elements into a set of unique values.
- Check if the set contains any integer or numeric values using the any() function and the isdigit() string method. The any() function returns True if at least one element in an iterable is True, and False otherwise.
- If the set does not contain any integers or numeric values, append the original sublist to a new list res.
- Print the filtered sublists.
Python3
test_list = [[ 4 , 'Gfg' , 'best' ], [ 'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
res = []
for sublist in test_list:
set_sublist = set (sublist)
if not any ( str (element).isdigit() or isinstance (element, int ) for element in set_sublist):
res.append(sublist)
print ( "The filtered rows: " , res)
|
Output
The filtered rows: [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity: O(n*m), where n is the length of test_list and m is the length of the longest sublist in test_list.
Auxiliary space: O(m), because it uses a set to store each sublist and the size of each set is at most the length of the longest sublist in test_list.
Method 7: using reduce():
- Import the reduce() function from the functools module.
- Define the list of lists test_list.
- Define a lambda function that takes an accumulator acc and a current list x as input. If all elements in x are non-numeric strings, the lambda function appends x to acc. Otherwise, it returns acc as is.
- Use reduce() to apply the lambda function to each sublist in test_list. The initial value of the accumulator is an empty list.
- Store the resulting filtered list in the variable res.
- Print the original list and the filtered list.
Python3
from functools import reduce
test_list = [[ 4 , 'Gfg' , 'best' ], [
'gfg' , 'is' , 'best' ], [ 3 , 5 ], [ 'GFG' , 'Best' ]]
res = reduce ( lambda acc, x: acc + [x] if all ( isinstance (ele, str )
and not ele.isnumeric() for ele in x) else acc, test_list, [])
print ( "The original list is : " + str (test_list))
print ( "The filtered rows : " + str (res))
|
Output
The original list is : [[4, 'Gfg', 'best'], ['gfg', 'is', 'best'], [3, 5], ['GFG', 'Best']]
The filtered rows : [['gfg', 'is', 'best'], ['GFG', 'Best']]
Time complexity:
The lambda function is applied to each sublist in test_list exactly once. The time complexity of the lambda function is O(m), where m is the length of the sublist. Therefore, the time complexity of the reduce() function is O(nm), where n is the length of test_list.
Space complexity:
The space complexity of this code is O(n), where n is the length of test_list. This is because we create a new list for each sublist that satisfies the condition in the lambda function, and the maximum number of sublists that can satisfy this condition is n
Share your thoughts in the comments
Please Login to comment...