微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

我写了这段代码,但我的输出不是应该是 2D 的,我不知道如何修复它

如何解决我写了这段代码,但我的输出不是应该是 2D 的,我不知道如何修复它

我完全编写了代码,但我没有从问题中得到预期的输出。当我输入我的值时,它们出于某种原因不在二维列表中。

功能

def matrix_rotate_right(a):

    b = []

    for i in range(len(a[0])):
        b.append([])
        for j in range(len(a) - 1,-1,-1):
            b[i].append(a[j][i])

    return b

t01.py

a = [[1,2,3],[4,5,6],[7,8,9],[10,11,12]]
b = matrix_rotate_right(a)
print(b)

问题:

Write and test the following function:

def matrix_rotate_right(a):
    """
    -------------------------------------------------------
    Returns a copy of a 2D matrix rotated to the right.
    a must be unchanged.
    Use: b = matrix_rotate_right(a)
    -------------------------------------------------------
    Parameters:
        a - a 2D list of values (2d list of int/float)
    Returns:
        b - the rotated 2D list of values (2D list of int/float)
    -------------------------------------------------------
    """
Add this function to the PyDev module functions.py. Test it from t01.py.

示例运行:

Given the following 2D matrix:

 1  2  3
 4  5  6
 7  8  9
10 11 12
Rotating it to the right produces the following matrix:

10  7  4  1
11  8  5  2
12  9  6  3

我得到的输出

[[10,7,4,1],[11,2],[12,9,6,3]]

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


Selenium Web驱动程序和Java。元素在(x,y)点处不可单击。其他元素将获得点击?
Python-如何使用点“。” 访问字典成员?
Java 字符串是不可变的。到底是什么意思?
Java中的“ final”关键字如何工作?(我仍然可以修改对象。)
“loop:”在Java代码中。这是什么,为什么要编译?
java.lang.ClassNotFoundException:sun.jdbc.odbc.JdbcOdbcDriver发生异常。为什么?
这是用Java进行XML解析的最佳库。
Java的PriorityQueue的内置迭代器不会以任何特定顺序遍历数据结构。为什么?
如何在Java中聆听按键时移动图像。
Java“Program to an interface”。这是什么意思?