Python - Sort row by K multiples
Last Updated :
08 Mar, 2023
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.
Input : test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]], K = 2
Output : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]]
Explanation : 0 < 2 = 2 < 4, multiple of 2 occurrence order.
Method #1 : Using sort() + % operator + len()
In this, we test for multiple using % operator and then compute count by getting length of filtered elements, provided to key to sort() which performs inplace sorting of rows.
Python3
# Python3 code to demonstrate working of
# Sort row by K multiples
# Using sort() + % operator + len()
# checking for multiples count
def k_mul(row):
return len([ele for ele in row if ele % K == 0])
# initializing list
test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
# performing sort
test_list.sort(key=k_mul)
# printing result
print("Sorted result : " + str(test_list))
Output:
The original list is : [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
Sorted result : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]]
Time Complexity: O(nlogn*mlogm)
Auxiliary Space: O(1)
Method #2 : Using sorted() + lambda + len()
In this, sorting is done using sorted(), len() is used to get length of all the multiples of K as in above method. The lambda function provides single statement alternative to perform logical injection.
Python3
# Python3 code to demonstrate working of
# Sort row by K multiples
# Using sorted() + lambda + len()
# initializing list
test_list = [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
# printing original list
print("The original list is : " + str(test_list))
# initializing K
K = 4
# performing sort using sorted()
# lambda avoiding external function call
res = sorted(test_list, key=lambda row: len(
[ele for ele in row if ele % K == 0]))
# printing result
print("Sorted result : " + str(res))
Output:
The original list is : [[3, 4, 8, 1], [12, 32, 4, 16], [1, 2, 3, 4], [9, 7, 5]]
Sorted result : [[9, 7, 5], [1, 2, 3, 4], [3, 4, 8, 1], [12, 32, 4, 16]]
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), as we’re using additional space other than the input list itself.
Similar Reads
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
Python - Sort Records by Kth Index List Sometimes, while working with Python Records, we can have a problem in which we need to perform Sorting of Records by some element in Tuple, this can again be sometimes, a list and sorting has to performed by Kth index of that list. This is uncommon problem, but can have usecase in domains such as w
4 min read
Python - Filter Sorted Rows 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,
3 min read
PySpark RDD - Sort by Multiple Columns In this article, we are going to learn sorting Pyspark RDD by multiple columns in Python. There occurs various situations in being a data scientist when you get unsorted data and there is not only one column unsorted but multiple columns are unsorted. This situation can be overcome by sorting the da
7 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