Given a Matrix, the task is to write a Python program that can sort its rows or columns on a measure of the number of values equal to its index number. For each row or column, count occurrences of equality of index number with value. After computation of this count for each row or column, sort the matrix on basis of the count extracted for each row or column in step 1.
Input : test_list = [[3, 1, 2, 5, 4], [0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Output : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [3, 1, 2, 5, 4], [0, 1, 2, 3, 4]]
Explanation : 0 < 2 < 3 < 5 is ordering for count of index-element equality. 2 is equal to its index position in 2nd list.
Input : test_list = [[0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Output : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [0, 1, 2, 3, 4]]
Explanation : 0 < 2 < 5 is ordering for count of index-element equality. All elements in last list are equal to its index hence the returned count.
Sorting rows
Method 1 : Using sort(), len() and enumerate()
In this, we check for index and element equality by comparing enumerate components, the summation of result is done by extracting length of list. This is passed as key to sort() which performs inplace sorting.
Example:
Python3
def get_idx_ele_count(row):
return len ([ele for idx, ele in enumerate (row) if ele = = idx])
test_list = [[ 3 , 1 , 2 , 5 , 4 ], [ 0 , 1 , 2 , 3 , 4 ],
[ 6 , 5 , 4 , 3 , 2 ], [ 0 , 5 , 4 , 2 ]]
print ( "The original list is : " + str (test_list))
test_list.sort(key = get_idx_ele_count)
print ( "Sorted List : " + str (test_list))
|
Output:
The original list is : [[3, 1, 2, 5, 4], [0, 1, 2, 3, 4], [6, 5, 4, 3, 2], [0, 5, 4, 2]]
Sorted List : [[6, 5, 4, 3, 2], [0, 5, 4, 2], [3, 1, 2, 5, 4], [0, 1, 2, 3, 4]]
Time Complexity: O(n*nlogn)
Auxiliary Space: O(n)
Method 2 : Using sorted(), lambda, len() and enumerate()
In this, we perform task of sorting using sorted() and lambda function provides utility to sort combined with len() and enumerate().
Example:
Python3
test_list = [[ 3 , 1 , 2 , 5 ], [ 0 , 1 , 2 , 3 ],
[ 6 , 5 , 4 , 3 ], [ 0 , 5 , 4 , 2 ]]
print ( "The original list is : " + str (test_list))
res = sorted (test_list, key = lambda row: len (
[ele for idx, ele in enumerate (row) if ele = = idx]))
print ( "Sorted List : " + str (res))
|
Output:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[6, 5, 4, 3], [0, 5, 4, 2], [3, 1, 2, 5], [0, 1, 2, 3]]
Sorting columns
Method 1 : Using sort(), len() and enumerate()
In this, we perform transpose of base matrix, and then perform usual task of getting index-value equality count using above method 1, after sorting, matrix is again converted to its transposed format.
Example:
Python3
def get_idx_ele_count(row):
return len ([ele for idx, ele in enumerate (row) if ele = = idx])
test_list = [[ 3 , 1 , 2 , 5 ], [ 0 , 1 , 2 , 3 ],
[ 6 , 5 , 4 , 3 ], [ 0 , 5 , 4 , 2 ]]
print ( "The original list is : " + str (test_list))
test_list = list ( zip ( * test_list))
test_list.sort(key = get_idx_ele_count)
test_list = zip ( * test_list)
res = [ list (sub) for sub in test_list]
print ( "Sorted List : " + str (res))
|
Output:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[3, 2, 5, 1], [0, 2, 3, 1], [6, 4, 3, 5], [0, 4, 2, 5]]
Time Complexity: O(n*logn)
Space Complexity: O(n)
Method 2 : Using sorted(), lambda, len() and enumerate()
In this, we perform transpose of base matrix, and then perform usual task of getting index-value equality count using above method 2, after sorting, matrix is again converted to its transposed format.
Example:
Python3
test_list = [[ 3 , 1 , 2 , 5 ], [ 0 , 1 , 2 , 3 ],
[ 6 , 5 , 4 , 3 ], [ 0 , 5 , 4 , 2 ]]
print ( "The original list is : " + str (test_list))
test_list = zip ( * test_list)
res = sorted (test_list, key = lambda row: len (
[ele for idx, ele in enumerate (row) if ele = = idx]))
res = zip ( * res)
res = [ list (sub) for sub in res]
print ( "Sorted List : " + str (res))
|
Output:
The original list is : [[3, 1, 2, 5], [0, 1, 2, 3], [6, 5, 4, 3], [0, 5, 4, 2]]
Sorted List : [[3, 2, 5, 1], [0, 2, 3, 1], [6, 4, 3, 5], [0, 4, 2, 5]]
Please Login to comment...