
- Python Basic Tutorial
- Python - Home
- Python - Overview
- Python - Environment Setup
- Python - Basic Syntax
- Python - Comments
- Python - Variables
- Python - Data Types
- Python - Operators
- Python - Decision Making
- Python - Loops
- Python - Numbers
- Python - Strings
- Python - Lists
- Python - Tuples
- Python - Dictionary
- Python - Date & Time
- Python - Functions
- Python - Modules
- Python - Files I/O
- Python - Exceptions
Python map function to find the row with the maximum number of 1’s
In this tutorial, we are going to write a program which finds a row with a maximum number of 1's from a matrix using the map function.
Let's say we have the following matrix.
matrix = [ [0, 0, 1], [1, 1, 1], [1, 1, 0] ]
We can write a program in different ways. But, using map function, we will follow the below procedure.
- Initialise the matrix.
- Find the number of 1's in every row using map function. Store them in a list.
- Print the max from the list.
Example
## initializing the matrix matrix = [ [0, 0, 1], [1, 1, 1], [1, 1, 0] ] ## function to find number of 1's in a row def number_of_ones(row): count = 0 for i in row: if i is 1: count += 1 return count ## finding the number of 1's in every row ## map returns an object which we converted into a list ones_count = list(map(number_of_ones, matrix)) ## printing the index of max number from the list print(ones_count.index(max(ones_count)))
Output
If you run the above program, you will get the following result.
1
If you have any doubts regarding the program, please do mention them in the comment section.
- Related Articles
- Python program using map function to find row with maximum number of 1's
- Python program using the map function to find a row with the maximum number of 1's
- Maximum length of consecutive 1’s in a binary string in Python using Map function
- Find the row with maximum number of 1s using C++
- Program to find the nth row of Pascal's Triangle in Python
- Find the maximum element of each row in a matrix using Python
- How to find the row that has maximum number of duplicates in an R matrix?
- What is the use of the map function in Python?
- Find the maximum number of composite summands of a number in Python
- Swift Program to find the 1's complement of the given number
- Haskell Program to find the 1's complement of the given number
- Find the largest rectangle of 1’s with swapping of columns allowed in Python
- Find row number of a binary matrix having maximum number of 1s in C++
- Return the maximum number in each array using map JavaScript
- Program to find number of square submatrices with 1 in python

Advertisements