【问题标题】:writing a program to find the sum of all even numbers in a matrix and find the transpose of the matrix [closed]编写一个程序来查找矩阵中所有偶数的总和并找到矩阵的转置[关闭]
【发布时间】:2020-12-12 22:22:03
【问题描述】:

这是给出的示例矩阵:

2 3 5
4 6 9
3 1 10

我要写两个程序,一个是求矩阵中所有偶数的总和,一个是求矩阵的转置(但这并不是说你不能编写一个程序)

据我所知,转置矩阵是指切换行和列,所以上面的矩阵看起来像这样

2 4 3
3 6 1
5 9 10

现在我只有为转置部分编写的代码,因为我什至不知道从哪里开始添加偶数部分:

a = [[2, 3, 5],
     [4, 6, 9],
     [3, 1, 10]]
n=len(a)
def transpose(A, B):
    for i in range(N):
        for j in range(N):
            B[i][j] = A[j][i]
print(a)

【问题讨论】:

标签: python python-3.x matrix transpose


【解决方案1】:

偶数之和:

sum(it for row in a for it in row if it % 2 == 0)

转置:

[*zip(*a)]

【讨论】:

    猜你喜欢
    • 2021-05-23
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-02-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多