Python 我写了这段代码,但我的输出不是2D格式,我不知道';我不知道如何修理它
我完整地编写了代码,但是我没有从问题中得到预期的结果。当我输入值时,由于某种原因,它们不在2D列表中 功能:Python 我写了这段代码,但我的输出不是2D格式,我不知道';我不知道如何修理它,python,Python,我完整地编写了代码,但是我没有从问题中得到预期的结果。当我输入值时,由于某种原因,它们不在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 Write and
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
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, 8, 5, 2], [12, 9, 6, 3]]
t01.py
a = [[1, 2, 3], [4, 5, 6], [7, 8, 9], [10, 11, 12]]
b = matrix_rotate_right(a)
print(b)
问题:
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
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, 8, 5, 2], [12, 9, 6, 3]]
运行示例:
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
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, 8, 5, 2], [12, 9, 6, 3]]
我得到的输出:
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
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, 8, 5, 2], [12, 9, 6, 3]]
你的4个功能实现了你想要的 您只需要格式化输出
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))))
在我看来,你已经归还了你想要的东西?这看起来像是一个漂亮的印刷品。试试看,你的任务就是旋转矩阵。它如何打印是无关紧要的。你的代码可以工作。