简体   繁体   English

我写了这段代码,但我的 output 不是应有的二维,我不知道如何修复它

[英]I wrote this code but my output is not in 2D as it should have been, and I don't know how to fix it

I wrote my code completely, but I'm not getting the expected output from the question.我完全编写了我的代码,但我没有从问题中得到预期的 output 。 When I put in my values they are not in a 2D list for some reason.当我输入我的值时,由于某种原因它们不在二维列表中。

Functions:功能:

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 t01.py

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

the question:问题:

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.

A sample run:示例运行:

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

The output I'm getting: output 我得到:

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

Your4 function doeswhat you wanted. Your4 function 可以满足您的需求。

You need only to format the output您只需格式化 output

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
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
b = matrix_rotate_right(a)
for tuple in b:
    print(format(' '.join(map(str,tuple))))

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Python:我的代码出错,我不知道如何修复它 - Python: Error in my code and i don't know how to fix it 由于 Python 程序中的 input() ,我收到了 EOFError 。 我什至不知道为什么我有它。 我该如何解决? - I get an EOFError because of an input() I have in my Python program. I don't even know why I have it. How should I fix it? 我有一个 python TypeError,我不知道如何修复它 - I have a python TypeError and I don't know how to fix it 我想扩展我的代码,但我不知道如何去做 - I'd like to extend my code however I don't know how to go about it 我的代码正在使用递归产生逻辑错误,但我不知道如何解决 - My code is producing a logic error with recursion and I don't know how to fix it 我写了一个快速排序的代码。 但我不知道为什么会发生 NameError? - I wrote a code for quicksort. But I don't know why this is occurring NameError? 我弄坏了我的诗歌装置,不知道从哪里开始修复它? - I have broken my poetry installation and don't know where to begin to fix it? 我有错误 TypeError: unsupported operand type(s) for /: 'function' and 'int' 我不知道如何修复它 - I have the error, TypeError: unsupported operand type(s) for /: 'function' and 'int' and I don't know how to fix it 我的代码有语法错误,我不知道问题是什么? - I have syntax error on my code and I don't know what the problem is? 这是我的计算机项目,我遇到错误,我不知道如何修复 - This is my Computer project and i am getting error and i don't know how to fix
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM