Find diagonals in a Python matrix using lists

Find diagonals in a Python matrix using lists

To find the diagonals in a matrix represented as a list of lists in Python, you need to extract two types of diagonals:

  1. Primary diagonals (top-left to bottom-right)
  2. Secondary diagonals (top-right to bottom-left)

Steps to Find Diagonals

  1. Primary Diagonals: Traverse from the top-left to bottom-right.
  2. Secondary Diagonals: Traverse from the top-right to bottom-left.

Here's a detailed implementation:

1. Primary Diagonals

To find all primary diagonals in a matrix:

def get_primary_diagonals(matrix): if not matrix or not matrix[0]: return [] rows = len(matrix) cols = len(matrix[0]) diagonals = [] # From top row for col in range(cols): diagonal = [] r, c = 0, col while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) # From left column (excluding the top-left corner) for row in range(1, rows): diagonal = [] r, c = row, 0 while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) return diagonals # Example matrix matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print("Primary diagonals:") for diag in get_primary_diagonals(matrix): print(diag) 

2. Secondary Diagonals

To find all secondary diagonals in a matrix:

def get_secondary_diagonals(matrix): if not matrix or not matrix[0]: return [] rows = len(matrix) cols = len(matrix[0]) diagonals = [] # From top row for col in range(cols): diagonal = [] r, c = 0, col while r < rows and c >= 0: diagonal.append(matrix[r][c]) r += 1 c -= 1 diagonals.append(diagonal) # From right column (excluding the top-right corner) for row in range(1, rows): diagonal = [] r, c = row, cols - 1 while r < rows and c >= 0: diagonal.append(matrix[r][c]) r += 1 c -= 1 diagonals.append(diagonal) return diagonals # Example matrix matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print("Secondary diagonals:") for diag in get_secondary_diagonals(matrix): print(diag) 

Explanation

  • Primary Diagonals: Start from each element in the top row and left column, collecting diagonals that go from top-left to bottom-right.
  • Secondary Diagonals: Start from each element in the top row and right column, collecting diagonals that go from top-right to bottom-left.

Output

For the example matrix:

matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] 

Primary Diagonals Output:

[1] [2, 4] [3, 5, 7] [6, 8] [9] 

Secondary Diagonals Output:

[1] [2, 4] [3, 5, 7] [6, 8] [9] 

You can adjust the matrix dimensions and contents to fit your needs. This approach will work for square and rectangular matrices.

Examples

  1. How to extract the main diagonal from a matrix in Python using lists?

    • Description: This code extracts the main diagonal (top-left to bottom-right) of a square matrix using lists.
    • Code:
      def main_diagonal(matrix): return [matrix[i][i] for i in range(len(matrix))] # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(main_diagonal(matrix)) # Output: [1, 5, 9] 
  2. How to find the anti-diagonal of a square matrix in Python using lists?

    • Description: This code finds the anti-diagonal (top-right to bottom-left) of a square matrix.
    • Code:
      def anti_diagonal(matrix): n = len(matrix) return [matrix[i][n - 1 - i] for i in range(n)] # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(anti_diagonal(matrix)) # Output: [3, 5, 7] 
  3. How to find all diagonals in a matrix (both main and anti-diagonals) in Python?

    • Description: This code finds both main and anti-diagonals of a square matrix.
    • Code:
      def all_diagonals(matrix): n = len(matrix) main_diag = [matrix[i][i] for i in range(n)] anti_diag = [matrix[i][n - 1 - i] for i in range(n)] return main_diag, anti_diag # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(all_diagonals(matrix)) # Output: ([1, 5, 9], [3, 5, 7]) 
  4. How to extract diagonals from a non-square matrix in Python?

    • Description: This code extracts diagonals from a non-square matrix, including diagonals starting from each row and column.
    • Code:
      def extract_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] # Get diagonals starting from each row for start in range(rows): diagonal = [] r, c = start, 0 while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) # Get diagonals starting from each column for start in range(1, cols): diagonal = [] r, c = 0, start while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] print(extract_diagonals(matrix)) # Output: [[1, 5, 9], [4, 8, 12], [7, 11], [10], [2, 6], [3]] 
  5. How to find the diagonals of a matrix with both main and anti-diagonals, including off-diagonals in Python?

    • Description: This code finds both main and anti-diagonals, as well as all off-diagonals.
    • Code:
      def all_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] # Collect main diagonals for d in range(-rows + 1, cols): diag = [] r, c = max(0, -d), max(0, d) while r < rows and c < cols: diag.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diag) # Collect anti-diagonals for d in range(rows + cols - 1): diag = [] r, c = min(d, rows - 1), max(0, d - (rows - 1)) while r >= 0 and c < cols: diag.append(matrix[r][c]) r -= 1 c += 1 diagonals.append(diag) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(all_diagonals(matrix)) # Output: [[7], [4, 8], [1, 5, 9], [2, 6], [3], [6], [5, 9], [9]] 
  6. How to get all diagonals of a rectangular matrix in Python using lists?

    • Description: This code retrieves all diagonals of a rectangular matrix, including those not confined to square matrices.
    • Code:
      def get_rectangular_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] for diag_start in range(-(rows - 1), cols): diagonal = [] r, c = max(0, -diag_start), max(0, diag_start) while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12] ] print(get_rectangular_diagonals(matrix)) # Output: [[10], [7, 11], [4, 8, 12], [1, 5, 9], [2, 6], [3]] 
  7. How to find the diagonal from a matrix where the diagonal starts from the top row in Python?

    • Description: This code finds all diagonals starting from the top row of a matrix.
    • Code:
      def top_row_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] for start_col in range(cols): diagonal = [] r, c = 0, start_col while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(top_row_diagonals(matrix)) # Output: [[1, 5, 9], [2, 6], [3]] 
  8. How to find the diagonal from a matrix where the diagonal starts from the left column in Python?

    • Description: This code retrieves all diagonals starting from the left column of a matrix.
    • Code:
      def left_column_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] for start_row in range(rows): diagonal = [] r, c = start_row, 0 while r < rows and c < cols: diagonal.append(matrix[r][c]) r += 1 c += 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(left_column_diagonals(matrix)) # Output: [[1, 5, 9], [4, 8], [7]] 
  9. How to find the diagonals in a matrix where the diagonal ends at the bottom row in Python?

    • Description: This code finds all diagonals that end at the bottom row of a matrix.
    • Code:
      def bottom_row_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] for start_col in range(cols): diagonal = [] r, c = rows - 1, start_col while r >= 0 and c < cols: diagonal.append(matrix[r][c]) r -= 1 c += 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(bottom_row_diagonals(matrix)) # Output: [[7], [4, 8], [1, 5, 9], [2, 6], [3]] 
  10. How to find the diagonals in a matrix where the diagonal starts from the right column in Python?

    • Description: This code retrieves all diagonals starting from the right column of a matrix.
    • Code:
      def right_column_diagonals(matrix): rows = len(matrix) cols = len(matrix[0]) diagonals = [] for start_row in range(rows): diagonal = [] r, c = start_row, cols - 1 while r < rows and c >= 0: diagonal.append(matrix[r][c]) r += 1 c -= 1 diagonals.append(diagonal) return diagonals # Example usage matrix = [ [1, 2, 3], [4, 5, 6], [7, 8, 9] ] print(right_column_diagonals(matrix)) # Output: [[3], [6, 8], [9, 5, 1], [6], [3]] 

More Tags

backslash dsl svg.js fopen connection-close integer-overflow database-dump pylint semantic-versioning html-agility-pack

More Programming Questions

More Retirement Calculators

More Housing Building Calculators

More Weather Calculators

More Tax and Salary Calculators