簡體   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

我完全編寫了我的代碼,但我沒有從問題中得到預期的 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

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

output 我得到:

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

Your4 function 可以滿足您的需求。

您只需格式化 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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM