Sunday, September 22, 2024
Google search engine
HomeLanguagesPython – Filter Rows with Range Elements

Python – Filter Rows with Range Elements

Given a Matrix, filter all the rows which contain all elements in the given number range.

Input : test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 
Output : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]] 
Explanation : 2, 3, 4, 5 all are present in above rows.

Input : test_list = [[3, 2, 4, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]], i, j = 2, 5 
Output : [[2, 3, 4, 5, 6, 7]] 
Explanation : 2, 3, 4, 5 all are present in above rows. 
 

Method #1 : Using all() + list comprehension

In this, we check for all the elements in range for presence using all() and list comprehension is used for the task of iteration of elements. 

Python3




# Python3 code to demonstrate working of
# Filter Rows with Range Elements
# Using all() + list comprehension
 
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
             [2, 5, 10], [2, 3, 4, 5, 6, 7]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
i, j = 2, 5
 
# checking for presence of all elements using in operator
res = [sub for sub in test_list if all(ele in sub for ele in range(i, j + 1))]
 
# printing result
print("Extracted rows : " + str(res))


Output

The original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]

Time Complexity: O(n)
Auxiliary Space: O(n)

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

In this, task of filtering is done using filter() and lambda function, all() is again used to ensure all elements presence in range.

Python3




# Python3 code to demonstrate working of
# Filter Rows with Range Elements
# Using filter() + lambda + all()
 
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
             [2, 5, 10], [2, 3, 4, 5, 6, 7]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
i, j = 2, 5
 
# filter() and lambda used to filter elements
res = list(filter(lambda sub: all(
    ele in sub for ele in range(i, j + 1)), test_list))
 
# printing result
print("Extracted rows : " + str(res))


Output

The original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]

Time Complexity: O(n*n) where n is the number of elements in the list “test_list”.  filter() + lambda + all() performs n*n number of operations.
Auxiliary Space: O(n), extra space is required where n is the number of elements in the list

Method #3:Using itertools.filterfalse() method

Python3




# Python3 code to demonstrate working of
# Filter Rows with Range Elements
import itertools
 
# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19],
             [2, 5, 10], [2, 3, 4, 5, 6, 7]]
 
# printing original list
print("The original list is : " + str(test_list))
 
# initializing range
i, j = 2, 5
 
 
res = list(itertools.filterfalse(lambda sub: not all(
    ele in sub for ele in range(i, j + 1)), test_list))
 
# printing result
print("Extracted rows : " + str(res))


Output

The original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows : [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]

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

Method #4:Using List Comprehension and Set Intersection:

Algorithm:

  1. Initialize the input list and range values.
  2. Use list comprehension to iterate through each sublist in the input list.
  3. Check if the set intersection of the sublist and the range values is equal to the range values set.
  4. If it is, append the sublist to the result list.
  5. Print the result list.

Python3




# initializing list
test_list = [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
# printing original list
print("The original list is : " + str(test_list))
i, j = 2, 5
res = [lst for lst in test_list if set(range(i, j+1)).intersection(lst) == set(range(i, j+1))]
print("Extracted rows: ", res)
#This code is contributed by Jyothi pinjala


Output

The original list is : [[3, 2, 4, 5, 10], [3, 2, 5, 19], [2, 5, 10], [2, 3, 4, 5, 6, 7]]
Extracted rows:  [[3, 2, 4, 5, 10], [2, 3, 4, 5, 6, 7]]

Time complexity:
The time complexity of this code is O(nm), where n is the number of sublists in the input list and m is the average length of each sublist. This is because we are iterating through each sublist and performing set operations on them.
Space complexity:
The space complexity of this code is O(k), where k is the length of the range between i and j. This is because we are using a set to store the range values.

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