Python 将输入矩阵与用户矩阵分离

Python 将输入矩阵与用户矩阵分离,python,python-3.x,matrix,Python,Python 3.x,Matrix,代码: 输入: lst = [] for _ in range(int(input())): T = int(input()) for i in range(T): matrix = list(map(int, input().split())) lst.append(matrix) print(lst) 输入此矩阵时,预期输出应为 [[1,2,3,4],[2,1,4,3],[3,4,1,2],[4,3,2,1]]等等,但该矩阵最终与其

代码:

输入:

lst = []
for _ in range(int(input())):
    T = int(input())
    for i in range(T):
        matrix = list(map(int, input().split()))
        lst.append(matrix)
    print(lst) 
输入此矩阵时,预期输出应为
[[1,2,3,4],[2,1,4,3],[3,4,1,2],[4,3,2,1]]
等等,但该矩阵最终与其他矩阵相加

我想检索
[[2,1,3],[1,3,2],[1,2,3]]
。获取<代码>[[1,2,3,4],[2,1,4,3],[3,4,1,2],[4,3,2,1],[2,2,2,2,3],[2,2,2,3],[2,2,2,2,2],[2,1,3],[1,3,2],[1,2,3]


我怎么能这么做呢?

你很接近了。不要将每个矩阵的行追加到列表中,而是将行追加到新矩阵中,然后将矩阵追加到列表中。差不多

3
4
1 2 3 4
2 1 4 3
3 4 1 2
4 3 2 1
4
2 2 2 2
2 3 2 3
2 2 2 3
2 2 2 2
3
2 1 3
1 3 2
1 2 3
现在
lst[-1]
将为您提供序列中的最后一个2D矩阵

lst = []
for _ in range(int(input())):  # Loop over number of matrices
    T = int(input())
    matrix = []
    for i in range(T):  # Loop over each row
        row = list(map(int, input().split()))
        matrix.append(row)
    lst.append(matrix)