# Python program to a Sort Matrix by index-value equality count

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

 `# Python3 code to demonstrate working of` `# Sort Matrix by index-value equality count` `# Using sort() + len() + enumerate()`     `def` `get_idx_ele_count(row):`   `    ``# getting required count` `    ``# element and index compared, if equal added` `    ``# in list, length computed using len()` `    ``return` `len``([ele ``for` `idx, ele ``in` `enumerate``(row) ``if` `ele ``=``=` `idx])`     `# initializing list` `test_list ``=` `[[``3``, ``1``, ``2``, ``5``, ``4``], [``0``, ``1``, ``2``, ``3``, ``4``], ` `             ``[``6``, ``5``, ``4``, ``3``, ``2``], [``0``, ``5``, ``4``, ``2``]]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# inplace sorting using sort()` `test_list.sort(key``=``get_idx_ele_count)`   `# printing result` `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

 `# Python3 code to demonstrate working of` `# Sort Matrix by index-value equality count` `# Using sorted() + lambda + len() + enumerate()`   `# initializing list` `test_list ``=` `[[``3``, ``1``, ``2``, ``5``], [``0``, ``1``, ``2``, ``3``], ` `             ``[``6``, ``5``, ``4``, ``3``], [``0``, ``5``, ``4``, ``2``]]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# sorting using sorted()` `# utility injection using lambda` `# element and index compared, if equal added in list,` `# length computed using len()` `res ``=` `sorted``(test_list, key``=``lambda` `row: ``len``(` `    ``[ele ``for` `idx, ele ``in` `enumerate``(row) ``if` `ele ``=``=` `idx]))`   `# printing result` `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

 `# Python3 code to demonstrate working of` `# Sort Matrix by index-value equality count` `# Using sort() + len() + enumerate()`     `def` `get_idx_ele_count(row):`   `    ``# getting required count` `    ``# element and index compared, if equal added in ` `    ``# list, length computed using len()` `    ``return` `len``([ele ``for` `idx, ele ``in` `enumerate``(row) ``if` `ele ``=``=` `idx])`     `# initializing list` `test_list ``=` `[[``3``, ``1``, ``2``, ``5``], [``0``, ``1``, ``2``, ``3``], ` `             ``[``6``, ``5``, ``4``, ``3``], [``0``, ``5``, ``4``, ``2``]]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# transposing matrix` `test_list ``=` `list``(``zip``(``*``test_list))`   `# inplace sorting using sort()` `test_list.sort(key``=``get_idx_ele_count)`   `# performing transpose again to get result.` `test_list ``=` `zip``(``*``test_list)`   `# converting list of tuples to list of lists` `res ``=` `[``list``(sub) ``for` `sub ``in` `test_list]`   `# printing result` `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

 `# Python3 code to demonstrate working of` `# Sort Matrix by index-value equality count` `# Using sorted() + lambda + len() + enumerate()`   `# initializing list` `test_list ``=` `[[``3``, ``1``, ``2``, ``5``], [``0``, ``1``, ``2``, ``3``],` `             ``[``6``, ``5``, ``4``, ``3``], [``0``, ``5``, ``4``, ``2``]]`   `# printing original list` `print``(``"The original list is : "` `+` `str``(test_list))`   `# transposing matrix` `test_list ``=` `zip``(``*``test_list)`   `# sorting using sorted()` `# utility injection using lambda` `# element and index compared, if equal added in` `# list, length computed using len()` `res ``=` `sorted``(test_list, key``=``lambda` `row: ``len``(` `    ``[ele ``for` `idx, ele ``in` `enumerate``(row) ``if` `ele ``=``=` `idx]))`   `# performing transpose again to get result.` `res ``=` `zip``(``*``res)`   `# converting list of tuples to list of lists` `res ``=` `[``list``(sub) ``for` `sub ``in` `res]`   `# printing result` `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]]

Whether you're preparing for your first job interview or aiming to upskill in this ever-evolving tech landscape, GeeksforGeeks Courses are your key to success. We provide top-quality content at affordable prices, all geared towards accelerating your growth in a time-bound manner. Join the millions we've already empowered, and we're here to do the same for you. Don't miss out - check it out now!

Previous
Next