- Trending Categories
Data Structure
Networking
RDBMS
Operating System
Java
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
Physics
Chemistry
Biology
Mathematics
English
Economics
Psychology
Social Studies
Fashion Studies
Legal Studies
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Program to convert each element in row and column to zero for zero values in Python
Suppose we have a 2D matrix of numbers, now for each zero in the given matrix and replace all values in its row and column with zero, and return the final matrix.
So, if the input is like matrix, then the output will be matrix as the 0th, 2nd and 3rd rows contain 0 and the final matrix contains 0 in those rows. Similarly 0th, 1st and 2nd columns contain 0 and the final matrix contains 0 in those columns.
To solve this, we will follow these steps:
n := row count, m := column count res := make a matrix of size n x m and fill with 0 transpose := transpose given matrix for each row i, do if 0 not in matrix[i], then for each column j in matrix, do if 0 not in transpose[j], then res[i, j] := matrix[i, j] return res
Let us see the following implementation to get better understanding −
Example
class Solution: def solve(self, matrix): n, m = len(matrix), len(matrix[0]) res = [[0 for __ in range(m)] for _ in range(n)] transpose = [list(row) for row in zip(*matrix)] for i in range(n): if 0 not in matrix[i]: for j in range(m): if 0 not in transpose[j]: res[i][j] = matrix[i][j] return res ob = Solution() matrix = [ [6, 0, 0, 6, 9], [4, 9, 9, 4, 8], [0, 8, 3, 4, 2], [9, 0, 7, 8, 3], [5, 2, 9, 6, 8] ] print(ob.solve(matrix))
Input
matrix = [ [6, 0, 0, 6, 9], [4, 9, 9, 4, 8], [0, 8, 3, 4, 2], [9, 0, 7, 8, 3], [5, 2, 9, 6, 8] ]
Output
[[0, 0, 0, 0, 0], [0, 0, 0, 4, 8], [0, 0, 0, 0, 0], [0, 0, 0, 0, 0], [0, 0, 0, 6, 8]]
- Related Articles
- How to create a column of first non-zero value in each row of an R data frame?
- How to create a column of first non-zero value in each row of a data.table object in R?
- Double the first element and move zero to end in C++ Program
- Python - Add a zero column to Pandas DataFrame
- How to add a leading zero to some values in a column in MySQL?
- How to create a vector with zero values in R Program?
- Replace NaN with zero and fill positive infinity for complex input values in Python
- Replace NaN with zero and fill negative infinity for complex input values in Python
- Program to find minimum operations to reduce X to zero in Python
- Replace NaN with zero and fill positive infinity values in Python
- Replace NaN with zero and fill negative infinity values in Python
- Program to count number of operations to convert binary matrix to zero matrix in C++
- Replace NaN with zero and infinity with large finite numbers for complex input values in Python
- How to find the average of non-zero values in a Python dictionary?
- Program to find smallest intersecting element of each row in a matrix in Python

Advertisements