Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/356.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 将矩阵旋转90度_Python_Algorithm_Multidimensional Array_Data Structures - Fatal编程技术网

Python 将矩阵旋转90度

Python 将矩阵旋转90度,python,algorithm,multidimensional-array,data-structures,Python,Algorithm,Multidimensional Array,Data Structures,背景 我正在解决一个标准。我知道有很多解决方案,但我正试图用一种对我有意义的方法来解决这个问题。我基本上是想把矩阵旋转90度。我试图交换方阵每一层的每一个值 这是我的密码: def rotate_image(matrix): top = 0 bottom = len(matrix) left = 0 right = len(matrix[0]) total_layers = round(bottom / 2) for i in range(0,

背景

我正在解决一个标准。我知道有很多解决方案,但我正试图用一种对我有意义的方法来解决这个问题。我基本上是想把矩阵旋转90度。我试图交换方阵每一层的每一个值

这是我的密码:

def rotate_image(matrix):
    top = 0
    bottom = len(matrix)
    left = 0
    right = len(matrix[0])
    total_layers = round(bottom / 2)

    for i in range(0, total_layers):

        for j in range(left, right - 1):
            top_left = matrix[top][j]
            top_right = matrix[j][right - 1]
            bottom_right = matrix[bottom - 1][right - (j + 1)]
            bottom_left = matrix[bottom - (1+j)][left]
            matrix[top][j] = bottom_left
            matrix[j][right - 1] = top_left
            matrix[bottom - 1][right - (j + 1)] = top_right
            matrix[bottom - (1 + j)][left] = bottom_right
        top += 1
        left += 1
        right -= 1
        bottom -= 1
    print(matrix)
出于某种原因,我的代码通过了大多数情况,但当我尝试以下情况时

print(rotate_image([[5, 1, 9, 11], [2, 4, 8, 10], [13, 3, 6, 7], [15, 14, 12, 16]]))
它失败了。我花了2个小时试着调试它,我可能是看了一些东西,但不确定它到底是什么


我希望得到一些反馈。

我可以为您提供另一种干净的方法,将方形矩阵旋转90度

步骤1:交换对角线上的元素。 步骤2:水平镜像元素

你得到了你的旋转矩阵

此外,您还可以根据需要旋转矩阵的方向,围绕水平镜像和垂直镜像进行播放

e、 g


您得到了90°旋转矩阵

我可以为您提供另一种干净的方法,将方形矩阵旋转90度

步骤1:交换对角线上的元素。 步骤2:水平镜像元素

你得到了你的旋转矩阵

此外,您还可以根据需要旋转矩阵的方向,围绕水平镜像和垂直镜像进行播放

e、 g


您得到了90°旋转矩阵

下一种方法选择矩阵的左上四分之一,存储来自该四分之一的元素,并以循环方式重新排列来自其他四分之一的相应元素

def rotate_image(matrix):
    n = len(matrix)
    for i in range(0, (n + 1) // 2):
        for j in range(0, n // 2):
            storage = matrix[i][j]
            matrix[i][j] = matrix[n - j - 1][i]
            matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
            matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
            matrix[j][n - i - 1] = storage
    for i in range(len(matrix)):
        print(matrix[i])

mt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

rotate_image(mt)
print()

mt = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21,22, 23,24,25]]

rotate_image(mt)

[13, 9, 5, 1]
[14, 10, 6, 2]
[15, 11, 7, 3]
[16, 12, 8, 4]

[21, 16, 11, 6, 1]
[22, 17, 12, 7, 2]
[23, 18, 13, 8, 3]
[24, 19, 14, 9, 4]
[25, 20, 15, 10, 5]

下一种方法选择矩阵的左上四分之一,存储来自该四分之一的元素,并以循环方式重新排列来自其他四分之一的相应元素

def rotate_image(matrix):
    n = len(matrix)
    for i in range(0, (n + 1) // 2):
        for j in range(0, n // 2):
            storage = matrix[i][j]
            matrix[i][j] = matrix[n - j - 1][i]
            matrix[n - j - 1][i] = matrix[n - i - 1][n - j - 1]
            matrix[n - i - 1][n - j - 1] = matrix[j][n - i - 1]
            matrix[j][n - i - 1] = storage
    for i in range(len(matrix)):
        print(matrix[i])

mt = [[1, 2, 3, 4], [5, 6, 7, 8], [9, 10, 11, 12], [13, 14, 15, 16]]

rotate_image(mt)
print()

mt = [[1, 2, 3, 4, 5], [6, 7, 8, 9, 10], [11, 12, 13, 14, 15], [16, 17, 18, 19, 20], [21,22, 23,24,25]]

rotate_image(mt)

[13, 9, 5, 1]
[14, 10, 6, 2]
[15, 11, 7, 3]
[16, 12, 8, 4]

[21, 16, 11, 6, 1]
[22, 17, 12, 7, 2]
[23, 18, 13, 8, 3]
[24, 19, 14, 9, 4]
[25, 20, 15, 10, 5]

您的数据库存在索引问题。让我们检查一下您的部分代码:

for j in range(left, right - 1):
    top_left = matrix[top][j]
    top_right = matrix[j][right - 1]
    bottom_right = matrix[bottom - 1][right - (j + 1)]
考虑当j取右-2时会发生什么。您将从列right-j+1=right-right-2+1=1中提取一个条目。也就是说,无论您处于哪个层,都将从第1列中提取。您需要调整回图层的起点,而不是右-j+1列,您需要访问左+右-j+1列。此外,请注意左+右实际上是一个常数,它等于矩阵的长度。您可以利用Python中的负索引,转而访问-j+1列

另外,我们已经被告知它是一个正方形矩阵,所以顶部和左侧总是相同的,底部和右侧总是相等的

下面是一段代码,用于执行我上面提到的更正:

def rotate(self, matrix: List[List[int]]) -> None:
    """
    Do not return anything, modify matrix in-place instead.
    """
    top = 0
    bottom = len(matrix)
    total_layers = round(bottom / 2)

    for i in range(0, total_layers):
        for j in range(top, bottom - 1):
            top_left = matrix[top][j]
            top_right = matrix[j][bottom - 1]
            bottom_right = matrix[bottom - 1][ - (j + 1)]
            bottom_left = matrix[- (1+j)][top]
            matrix[top][j] = bottom_left
            matrix[j][bottom-1] = top_left 
            matrix[bottom - 1][- (j + 1)] = top_right
            matrix[- (1 + j)][top] = bottom_right
        top += 1
        bottom -= 1

您的数据库存在索引问题。让我们检查一下您的部分代码:

for j in range(left, right - 1):
    top_left = matrix[top][j]
    top_right = matrix[j][right - 1]
    bottom_right = matrix[bottom - 1][right - (j + 1)]
考虑当j取右-2时会发生什么。您将从列right-j+1=right-right-2+1=1中提取一个条目。也就是说,无论您处于哪个层,都将从第1列中提取。您需要调整回图层的起点,而不是右-j+1列,您需要访问左+右-j+1列。此外,请注意左+右实际上是一个常数,它等于矩阵的长度。您可以利用Python中的负索引,转而访问-j+1列

另外,我们已经被告知它是一个正方形矩阵,所以顶部和左侧总是相同的,底部和右侧总是相等的

下面是一段代码,用于执行我上面提到的更正:

def rotate(self, matrix: List[List[int]]) -> None:
    """
    Do not return anything, modify matrix in-place instead.
    """
    top = 0
    bottom = len(matrix)
    total_layers = round(bottom / 2)

    for i in range(0, total_layers):
        for j in range(top, bottom - 1):
            top_left = matrix[top][j]
            top_right = matrix[j][bottom - 1]
            bottom_right = matrix[bottom - 1][ - (j + 1)]
            bottom_left = matrix[- (1+j)][top]
            matrix[top][j] = bottom_left
            matrix[j][bottom-1] = top_left 
            matrix[bottom - 1][- (j + 1)] = top_right
            matrix[- (1 + j)][top] = bottom_right
        top += 1
        bottom -= 1

用钢笔和纸为一个失败的小例子执行你的算法。在每一步后比较部分结果是否符合您对最终结果的预期。用钢笔和纸对一个小的失败示例执行您的算法。在每个步骤后比较部分结果是否符合您对最终结果的预期。这是迄今为止唯一一个试图帮助OP解决有关其代码问题的答案。这是迄今为止唯一一个试图帮助OP解决有关其代码问题的答案。