SoFunction
Updated on 2024-11-12

python implements 2D list transpose

python 2D list transpose

def transpose(self, matrix):
    new_matrix = []
    for i in range(len(matrix[0])):
      matrix1 = []
      for j in range(len(matrix)):
        (matrix[j][i])
      new_matrix.append(matrix1)
    return new_matrix

python 2D list transposed counterclockwise

def transpose(self, matrix):
    new_matrix = []
    for i in range(len(matrix[0])):
      matrix1 = []
      for j in range(len(matrix)):
        (matrix[j][i])
      new_matrix.append(matrix1)
    return new_matrix[::-1]

Example:

Enter a matrix and print each number in clockwise order from the outside in. For example, if you enter the following 4 X 4 matrix: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 then the numbers 1,2,3,4,8,12,16,15,14,13,9,5,6,7,11,10 will be printed in order.

# -*- coding:utf-8 -*-
class Solution:
  # matrix type is a two-dimensional list, need to return the list
  # matrix should be a list composed of lists
  # Remove the first line and transpose it counterclockwise
  def printMatrix(self, matrix):
    # write code here
    result = []
    while matrix:
      ((0))
      if not matrix:
        break
      matrix = (matrix)
    return result
  # Transfers
  def transpose(self, matrix):
    new_matrix = []
    for i in range(len(matrix[0])):
      matrix1 = []
      for j in range(len(matrix)):
        (matrix[j][i])
      new_matrix.append(matrix1)
    return new_matrix[::-1]

Above this python implementation of two-dimensional list transposition is all I have shared with you, I hope to give you a reference, and I hope you support me more.