如何解决我写了这段代码,但我的输出不是应该是 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 举报,一经查实,本站将立刻删除。