Saturday, November 16, 2024
Google search engine
HomeLanguagesPython – Extract Row with any Boolean True

Python – Extract Row with any Boolean True

Given a Boolean Matrix, extract a row that contains at least one boolean True value.

Input : test_list = [[False, False], [True, True, True], [False, True], [False]] 
Output : [[True, True, True], [False, True]] 
Explanation : All rows with atleast 1 True extracted.

Input : test_list = [[False, False], [False]] 
Output : [] 
Explanation : No rows with even one True. 
 

Method #1 : Using list comprehension + any()

In this, we check for any element to be boolean true using any() and list comprehension is used for task of iteration of rows in matrix.

Python3




# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# Using list comprehension + any()
 
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using any() to check for any True value
res = [sub for sub in test_list if any(ele for ele in sub)]
 
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

Time Complexity : O(n^2)

Space Complexity : O(n) 

Method #2: Using any() + filter() + lambda

In this, we perform the task of checking for any True value using any() and filter(), and lambda is used to filter out matching rows.

Python3




# Python3 code to demonstrate working of
# Extract Row with any Boolean True
# Using any() + filter() + lambda
 
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using any() to check for any True value
# filter() to perform filtering
res = list(filter(lambda sub: any(ele for ele in sub), test_list))
 
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

Time Complexity : O(n^2)

Space Complexity : O(n) 

Method #3 : Using count() method

Python3




# Python3 code to demonstrate working of
# Extract Row with any Boolean True
 
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
res=[]
for i in test_list:
    if(i.count(True)>=1):
        res.append(i)
 
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

Time Complexity : O(n*m)

Space Complexity : O(n) 

Method #4 : Using in operator

Python3




# Python3 code to demonstrate working of
# Extract Row with any Boolean True
 
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using any() to check for any True value
res=[]
for i in test_list:
    if True in i:
        res.append(i)
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

time complexity : O(n * m)

space complexity : O(n)

Method #5:Using itertools.filterfalse() method

Python3




# Python3 code to demonstrate working of
# Extract Row with any Boolean True
import itertools
# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
 
res = list(itertools.filterfalse(lambda sub: not any(ele for ele in sub), test_list))
 
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

Time Complexity:O(N*N)
Auxiliary Space:O(N*N)

Method 6 :  using a for loop and a temporary list to keep track of the rows that contain True.

 step-by-step  approach :

Initialize an empty list res to store the rows that contain True.
Loop over each row i in the test_list.
Check if any element in the row i is True using the any() function. If it is, append the row i to the res list.
After looping over all rows, print the res list to show the extracted rows.

Python3




# initializing list
test_list = [[False, False], [True, True, True], [False, True], [False]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# using for loop and temporary list to extract rows with any True value
res = []
for i in test_list:
    if any(i):
        res.append(i)
 
# printing result
print("Extracted Rows : " + str(res))


Output

The original list is : [[False, False], [True, True, True], [False, True], [False]]
Extracted Rows : [[True, True, True], [False, True]]

Time complexity: O(nm), where n is the number of rows and m is the maximum length of a row.
Auxiliary space: O(k), where k is the number of rows with at least one True value.

Dominic Rubhabha-Wardslaus
Dominic Rubhabha-Wardslaushttp://wardslaus.com
infosec,malicious & dos attacks generator, boot rom exploit philanthropist , wild hacker , game developer,
RELATED ARTICLES

Most Popular

Recent Comments