【问题标题】:matrix 90 degree clockwise rotation using list in python使用python中的列表矩阵顺时针旋转90度
【发布时间】:2018-12-22 12:28:30
【问题描述】:

我正在尝试在 python 中将 3*3 矩阵顺时针旋转 90 度。 我已经确定 [i][j] 处的元素转到 [j][new_i]。 这里 new_i 依赖于前一个 i,所以我为它创建了一个函数,称为循环减法。

如果 i 为 0,则 new_i 为 2

如果 i 为 1,则 new_i 为 1

如果 i 为 2,则 new_i 为 0

执行后,它给了我意想不到的结果。

我已经打印了每次迭代中发生的所有事情。 我无法弄清楚某些元素是如何被不同的元素替换的。

'''
1 2 3                       7 4 1
4 5 6  rotate 90 degrees    8 5 2
7 8 9                       9 6 3


'''

def circular_subtraction(i):
    new_i = i 
    if(i==0):
        new_i = 2
    elif(i==1):
        new_i = 1
    elif(i==2):
        new_i = 0
    return new_i


def rotate_clock(matrix):
    new_matrix = matrix

for i in range(len(matrix)):
    for j in range(len(matrix)):
        new_i = circular_subtraction(i)
        new_matrix[j][new_i] = matrix[i][j]
        print("New element added from {},{} to {},{} ::: {} to {}".format(i+1,j+1,j+1,new_i+1,matrix[i][j],new_matrix[j][new_i]))

for each_row in new_matrix:
    print(each_row)




matrix = [[1,2,3],[4,5,6],[7,8,9]]
print("Length of the matrix : ",len(matrix))
for each_row in matrix:
    print(each_row)
print()
matrix = rotate_clock(matrix)

输入矩阵是

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

预期结果是:

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

结果是:

[7, 4, 1]
[2, 5, 2]
[1, 2, 1]

【问题讨论】:

  • 请尝试指出上述代码中的错误。我知道它可以很容易地用 numpy 完成,但我想手动尝试。

标签: python list matrix


【解决方案1】:

你可以这样做:

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

rotated = [list(reversed(col)) for col in zip(*matrix)]

for row in rotated:
    print(*row)

输出

7 4 1
8 5 2
9 6 3

for col in zip(*matrix) 获取矩阵的列,一旦获得需要使用list(reversed(col)) 反转它们的列,然后使用list comprehension 将所有列放在一起:

rotated = [list(reversed(col)) for col in zip(*matrix)]

上面的列表推导等价于下面的 pythonic for 循环:

rotated = []
for col in zip(*matrix):
    rotated.append(list(reversed(col)))

进一步

  1. 有关zipreversedlist 的文档。
  2. 符号*matrix 被称为元组解包,更多的是here

【讨论】:

  • 所以我们旋转每一列然后转换成一行?
  • 是的,你得到每一列,反转它并转换成一行。
  • 太棒了。
  • 逆时针怎么做?
【解决方案2】:

您可以为此使用 numpy rot90 函数:np.rot90

mat = [[1, 2, 3], [4,5,6,], [7,8,9]]
np.rot90(mat, k=1, axes=(1,0))

k - 表示旋转次数
axes - 表示旋转方向

输出

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

代码中的问题

代码缺少缩进(但假设它们是正确的)
行:

new_matrix = matrix

为矩阵变量分配一个新的引用。
在 python 中,默认值不是按值复制。 您可以使用深拷贝功能:copy.deepcopy(x[, memo])

import copy

def rotate_clock(matrix):
    new_matrix = copy.deepcopy(matrix)

def rotate_clock(matrix):
    new_matrix = [row[:] for row in matrix]

否则,您对 new_matrix 所做的每次更改也会在原始矩阵中完成。 (因为 new_matrix 只是对矩阵的引用)

【讨论】:

  • 谢谢。这就是我想要的。
【解决方案3】:

在非常简单的python中,这段代码可以工作:

def rotate_matrix(a):
    b = []
    i = len(a)-1
    while i>=0:
        for j in range(0, len(a)):
            if (len(b) < (j+1)):
                b.append([a[i][j]])
            else:
                b[j].append(a[i][j])
        i -= 1
    return b

我打印了 b。看起来像这样: [[7, 4, 1], [8, 5, 2], [9, 6, 3]]

【讨论】:

    【解决方案4】:

    numpy.rot90 也可以派上用场:

    import numpy as np
    
    a = [[1, 2, 3],
         [4, 5, 6],
         [7, 8, 9]]
    a_rot = np.rot90(a, k=3).tolist()
    for row in a_rot:
      print(row)
    

    输出

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

    【讨论】:

      【解决方案5】:

      旋转Matrix 的通用方法,与形状无关。

      import numpy as np
      A=np.array([[1, 2, 3, 33], [4, 5, 6, 66], [7, 8, 9, 99]])
      A
      array([[ 1,  2,  3, 33],
             [ 4,  5,  6, 66],
             [ 7,  8,  9, 99]])
      
      rotated_A=np.zeros((len(A[0]),len(A)))
      for i in range(len(A)):
          for j in range(len(A[0])):
              rotated_A[j][len(A)-1-i]=A[i][j]
      rotated_A
      array([[  7.,   4.,   1.],
             [  8.,   5.,   2.],
             [  9.,   6.,   3.],
             [ 99.,  66.,  33.]])
      

      【讨论】:

        【解决方案6】:

        旋转任何矩阵的简单解决方案是

        import copy
        def rotateImage(a):
         out = copy.deepcopy(a)
         x = 0;
         y = 0;
         for i in a:
          l = len(i)
          for j in i:
           out[y][x+l-1] = j
           y += 1
           if(y == l):
            y=0
          x -= 1
         return(out)
        

        【讨论】:

          猜你喜欢
          • 2013-05-05
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2020-09-04
          • 1970-01-01
          • 2021-06-30
          • 1970-01-01
          相关资源
          最近更新 更多