- Trending Categories
- Data Structure
- Operating System
- C Programming
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Python Program to Sort Matrix Rows by summation of consecutive difference of elements
Below is a demonstration of the same
def diff_summation_elem(row): return sum([abs(row[index + 1] - row[index]) for index in range(0, len(row) - 1)]) my_list = [[97, 6, 47, 3], [6, 88, 3, 26], [71, 53, 34, 65], [15, 36, 5,62]] print("The list is : ") print(my_list) my_list.sort(key=diff_summation_elem) print("The resultant list is :" ) print(my_list)
The list is : [[97, 6, 47, 3], [6, 88, 3, 26], [71, 53, 34, 65], [15, 36, 5, 62]] The resultant list is : [[71, 53, 34, 65], [15, 36, 5, 62], [97, 6, 47, 3], [6, 88, 3, 26]]
A method named ‘diff_summation_elem’ is defined that takes a list as a parameter.
It uses the ‘abs’ method and the ‘sum’ method along with list comprehension to iterate over the list and get specific index values.
Outside the method, a list of list is defined and is displayed on the console.
The list is sorted based on the key being the method (previously defined).
The output is displayed on the console.
- Python – Summation of consecutive elements power
- Python Program to sort rows of a matrix by custom element count
- Python program to sort matrix based upon sum of rows
- Python - Sort rows by Frequency of K
- C program to sort all columns and rows of matrix
- Python Program to Extract Rows of a matrix with Even frequency Elements
- Program to sort array by increasing frequency of elements in Python
- Python program to sort tuples by frequency of their absolute difference
- Python – Sort Matrix by Number of elements greater than its previous element
- Python program to extract rows with common difference elements
- Maximum difference of sum of elements in two rows in a matrix in C
- Python – Group Consecutive elements by Sign
- Find distinct elements common to all rows of a matrix in Python
- Python – Sort Matrix by total characters
- Python – Sort Matrix by Row Median