python中矩阵的最大列和行总和 [英] Maximum column and row sum of matrix in python
本文介绍了python中矩阵的最大列和行总和的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我接受矩阵的输入
import numpy as np
l = np.array([input().split() for _ in range(3)], dtype=np.int)
1 2 3
4 5 6
7 8 9
现在,我想显示最大的金额.它可以在列或行中.
Now I want to display the largest sum. It can be in either columns or rows.
例如: 第3行的总和为24
For example in this: row 3 has maximum sum 24
所以我的输出将是: 第3行24
So my output will be: row 3 24
推荐答案
工作示例:
import numpy as np
x = np.array([[1,2,3],[4,5,6],[7,8,9]]);
rowSum = np.sum(x, axis=1)
colSum = np.sum(x, axis=0)
print("row {} {}".format(np.argmax(rowSum)+1, np.max(rowSum)))
print("col {} {}".format(np.argmax(colSum)+1, np.max(colSum)))
# output:
# row 3 24
# col 3 18
请参见
https://docs.scipy.org/doc/numpy-1.15.1/reference/generated/numpy.sum.html
这篇关于python中矩阵的最大列和行总和的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文