Python - Filter Sorted Rows
Last Updated :
08 Mar, 2023
Given a Matrix, extract rows that are sorted, either by ascending or descending.
Input : test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]]
Output : [[3, 6, 8, 10], [8, 5, 3, 2]]
Explanation : Both lists are ordered, 1st ascending, second descending.
Input : test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 7, 2], [1, 4, 5, 3]]
Output : [[3, 6, 8, 10]]
Explanation : List ordered in ascending order.
Method #1 : Using list comprehension + sorted() + reverse
In this, we check for each row, perform sort by sorted(), and reverse sorted by passing reverse as key.
Python3
# Python3 code to demonstrate working of
# Filter Sorted Rows
# Using list comprehension + sorted() + reverse
# initializing list
test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]]
# printing original list
print("The original list is : " + str(test_list))
# filtering using sorted() and reverse as key
res = [sub for sub in test_list if sub == list(
sorted(sub)) or sub == list(sorted(sub, reverse=True))]
# printing result
print("Extracted rows : " + str(res))
Output:
The original list is : [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]] Extracted rows : [[3, 6, 8, 10], [8, 5, 3, 2]]
Time Complexity: O(n*nlogn)
Auxiliary Space: O(n)
Method #2 : Using filter() + lambda + sorted() + reverse
In this, we perform task of filtering using lambda and sorted() and reverse can be used to check for equality with ordered list.
Python3
# Python3 code to demonstrate working of
# Filter Sorted Rows
# Using filter() + lambda + sorted() + reverse
# initializing list
test_list = [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]]
# printing original list
print("The original list is : " + str(test_list))
# filtering using sorted() and reverse as key
res = list(filter(lambda sub: sub == list(sorted(sub)) or sub ==
list(sorted(sub, reverse=True)), test_list))
# printing result
print("Extracted rows : " + str(res))
Output:
The original list is : [[3, 6, 8, 10], [1, 8, 2, 4, 3], [8, 5, 3, 2], [1, 4, 5, 3]] Extracted rows : [[3, 6, 8, 10], [8, 5, 3, 2]]
Time Complexity: O(n*logn), where n is the length of the input list. This is because we’re using the built-in sorted() function which has a time complexity of O(nlogn) 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.
Similar Reads
Python - Sort row by K multiples Given a Matrix, perform row sorting by number of multiple of K present in row. Input : test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]], K = 4 Output : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]] Explanation : 0 < 1 < 2 < 4, multiple of 4 occurrence order. I
3 min read
Sort a list in python Sorting is a fundamental operation in programming, allowing you to arrange data in a specific order. Here is a code snippet to give you an idea about sorting.Python# Initializing a list a = [5, 1, 5, 6] # Sort modifies the given list a.sort() print(a) b = [5, 2, 9, 6] # Sorted does not modify the gi
5 min read
Python - Sort rows by Frequency of K Given a Matrix, the task is to write a Python program to perform sorting on rows depending on the frequency of K. Input : test_list = [[10, 2, 3, 2, 3], [5, 5, 4, 7, 7, 4], [1, 2], [1, 1, 2, 2, 2]], K = 2 Output : [[5, 5, 4, 7, 7, 4], [1, 2], [10, 2, 3, 2, 3], [1, 1, 2, 2, 2]] Explanation : 0 < 1
4 min read
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 :
5 min read
Python - Sort Matrix by Row Median Given a Matrix, sort by median of each row. Input : test_list = [[3, 4, 7], [1, 7, 2], [10, 2, 4], [8, 6, 5]] Output : [[1, 7, 2], [3, 4, 7], [10, 2, 4], [8, 6, 5]] Explanation : 2 < 3 < 4 < 6, sorted increasingly by median element. Input : test_list = [[3, 4, 7], [1, 7, 2], [8, 6, 5]] Outp
4 min read