- 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 print a specific number of rows with Maximum Sum
When it is required to print a specific number of rows with the maximum sum, the ‘sorted’ method, and the ‘lambda’ method are used.
Below is a demonstration of the same
my_list = [[2, 4, 6, 7], [2, 4, 8], , [1, 3, 5, 6], [8, 2, 1]] print("The list is :") print(my_list) my_key = 3 print("The key is") print(my_key) my_result = sorted(my_list, key=lambda row: sum(row), reverse=True)[:my_key] print("The resultant list is :") print(my_result)
The list is : [[2, 4, 6, 7], [2, 4, 8], , [1, 3, 5, 6], [8, 2, 1]] The key is 3 The resultant list is : [, [2, 4, 6, 7], [1, 3, 5, 6]]
A list of list is defined and is displayed on the console.
A key value is defined and is displayed on the console.
The ‘sorted’ method is used on the list along with the lambda method, where the sum of the elements is determined, and the elements are reversed based on the key value.
This is assigned to a variable.
This is displayed as output on the console.
- Python Program to Filter Rows with a specific Pair Sum
- Maximum sum of pairs with specific difference C++ program
- Python - Sum only specific rows of a Pandas Dataframe
- Program to find sum of contiguous sublist with maximum sum in Python
- Python Pandas - Display specific number of rows from a DataFrame
- Program to find number of columns flips to get maximum number of equal Rows in Python?
- Maximum sum of pairs with specific difference in C++
- Python Program to print element with maximum vowels from a List
- Program to find maximum number of non-overlapping subarrays with sum equals target using Python
- Number of pairs with maximum sum in C++
- How can Tensorflow be used to sum specific elements/rows of a matrix in Python?
- Write a program in Python to print dataframe rows as orderDict with a list of tuple values
- Python program to print number triangle
- Python program to sort matrix based upon sum of rows
- Flip Columns For Maximum Number of Equal Rows in Python